mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-16 16:15:06 +00:00
feat: durability and idempotency keys (#2450)
* feat: durability and idempotency keys * feat: more durable frontend * fix: tests * fix: mini issues * fix: better json validation * fix: tests
This commit is contained in:
112 files changed
+13520
-614
No files matched your search
@@ -218,10 +218,19 @@ class TestCompleteStreamMethod:
|
||||
|
||||
decoded_token = {"sub": "user123"}
|
||||
|
||||
# The fresh-question path now reserves a row before agent.gen()
|
||||
# and calls finalize_message at end of stream — assert both fire.
|
||||
with patch.object(
|
||||
resource.conversation_service, "save_conversation"
|
||||
) as mock_save:
|
||||
mock_save.return_value = str(uuid.uuid4())
|
||||
resource.conversation_service, "save_user_question"
|
||||
) as mock_reserve, patch.object(
|
||||
resource.conversation_service, "finalize_message"
|
||||
) as mock_finalize:
|
||||
mock_reserve.return_value = {
|
||||
"conversation_id": str(uuid.uuid4()),
|
||||
"message_id": str(uuid.uuid4()),
|
||||
"request_id": "req-1",
|
||||
}
|
||||
mock_finalize.return_value = True
|
||||
|
||||
list(
|
||||
resource.complete_stream(
|
||||
@@ -234,7 +243,8 @@ class TestCompleteStreamMethod:
|
||||
)
|
||||
)
|
||||
|
||||
mock_save.assert_called_once()
|
||||
mock_reserve.assert_called_once()
|
||||
mock_finalize.assert_called_once()
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -45,19 +45,26 @@ class TestMakeSerializable:
|
||||
got = _make_serializable([u, {"x": u}, 1])
|
||||
assert got == [str(u), {"x": str(u)}, 1]
|
||||
|
||||
def test_bytes_decoded_to_string(self):
|
||||
def test_bytes_base64_encoded(self):
|
||||
# Migrated from UTF-8-replace to base64 once the helper moved to
|
||||
# the shared serialization module — base64 is lossless and round-
|
||||
# trippable (UTF-8-replace silently corrupted binary payloads).
|
||||
import base64
|
||||
from application.api.answer.services.continuation_service import (
|
||||
_make_serializable,
|
||||
)
|
||||
assert _make_serializable(b"hello") == "hello"
|
||||
got = _make_serializable(b"hello")
|
||||
assert got == base64.b64encode(b"hello").decode("ascii")
|
||||
|
||||
def test_bytes_invalid_utf8_replaced(self):
|
||||
def test_bytes_arbitrary_binary_roundtrips(self):
|
||||
import base64
|
||||
from application.api.answer.services.continuation_service import (
|
||||
_make_serializable,
|
||||
)
|
||||
# Invalid UTF-8 byte sequence
|
||||
got = _make_serializable(b"\xff\xfe")
|
||||
raw = b"\xff\xfe\x00\x10"
|
||||
got = _make_serializable(raw)
|
||||
assert isinstance(got, str)
|
||||
assert base64.b64decode(got) == raw
|
||||
|
||||
def test_passes_through_primitives(self):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
@@ -68,6 +75,50 @@ class TestMakeSerializable:
|
||||
assert _make_serializable(None) is None
|
||||
assert _make_serializable(True) is True
|
||||
|
||||
def test_datetime_becomes_iso_string(self):
|
||||
# PG SELECT * pulls timestamptz columns through as datetime —
|
||||
# tools_dict carries ``created_at``/``updated_at`` from user_tools
|
||||
# rows, which would otherwise blow up json.dumps in pending_tool_state.
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from application.api.answer.services.continuation_service import (
|
||||
_make_serializable,
|
||||
)
|
||||
|
||||
ts = datetime(2026, 5, 2, 12, 14, 32, tzinfo=timezone.utc)
|
||||
got = _make_serializable(ts)
|
||||
assert got == "2026-05-02T12:14:32+00:00"
|
||||
json.dumps(got) # would raise on raw datetime
|
||||
|
||||
def test_datetime_nested_in_tools_dict(self):
|
||||
# Mirrors the production failure: tools_dict is a dict-of-dicts
|
||||
# where each tool row has timestamp fields buried under string keys.
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from application.api.answer.services.continuation_service import (
|
||||
_make_serializable,
|
||||
)
|
||||
|
||||
ts = datetime(2026, 5, 2, 12, 14, 32, tzinfo=timezone.utc)
|
||||
tools_dict = {
|
||||
"0": {
|
||||
"name": "mcp_tool",
|
||||
"actions": [{"name": "search", "active": True}],
|
||||
"created_at": ts,
|
||||
"updated_at": ts,
|
||||
}
|
||||
}
|
||||
got = _make_serializable(tools_dict)
|
||||
json.dumps(got)
|
||||
assert got["0"]["created_at"] == "2026-05-02T12:14:32+00:00"
|
||||
|
||||
def test_date_becomes_iso_string(self):
|
||||
from datetime import date
|
||||
from application.api.answer.services.continuation_service import (
|
||||
_make_serializable,
|
||||
)
|
||||
assert _make_serializable(date(2026, 5, 2)) == "2026-05-02"
|
||||
|
||||
|
||||
class TestContinuationServiceSaveLoad:
|
||||
def test_save_and_load_state(self, pg_conn):
|
||||
|
||||
@@ -229,6 +229,489 @@ class TestConversationServiceSave:
|
||||
assert got["name"] == "q-fallback"
|
||||
|
||||
|
||||
class TestSaveUserQuestion:
|
||||
def test_creates_conversation_and_reserves_message(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
TERMINATED_RESPONSE_PLACEHOLDER,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-wal-new"
|
||||
with _patch_db(pg_conn):
|
||||
result = ConversationService().save_user_question(
|
||||
conversation_id=None,
|
||||
question="what is python?",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
assert result["conversation_id"]
|
||||
assert result["message_id"]
|
||||
assert result["request_id"]
|
||||
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
conv = repo.get_any(result["conversation_id"], user)
|
||||
assert conv is not None
|
||||
messages = repo.get_messages(result["conversation_id"])
|
||||
assert len(messages) == 1
|
||||
assert messages[0]["status"] == "pending"
|
||||
assert messages[0]["prompt"] == "what is python?"
|
||||
assert messages[0]["response"] == TERMINATED_RESPONSE_PLACEHOLDER
|
||||
assert messages[0]["request_id"] == result["request_id"]
|
||||
|
||||
def test_appends_to_existing_conversation(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-wal-existing"
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
conv = repo.create(user, name="hi")
|
||||
conv_id = str(conv["id"])
|
||||
|
||||
with _patch_db(pg_conn):
|
||||
result = ConversationService().save_user_question(
|
||||
conversation_id=conv_id,
|
||||
question="follow-up",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
assert result["conversation_id"] == conv_id
|
||||
msgs = repo.get_messages(conv_id)
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["prompt"] == "follow-up"
|
||||
|
||||
def test_raises_when_token_missing(self):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
with pytest.raises(ValueError):
|
||||
ConversationService().save_user_question(
|
||||
conversation_id=None,
|
||||
question="q",
|
||||
decoded_token=None,
|
||||
)
|
||||
|
||||
def test_regenerate_at_index_replaces_old_message(self, pg_conn):
|
||||
"""Regenerate at ``index`` truncates the old message *and
|
||||
everything after* before reserving the placeholder, so the new
|
||||
WAL row lands at ``position=index`` rather than appending at
|
||||
the end. Pre-fix the WAL path appended unconditionally and the
|
||||
old answer survived alongside the regenerated one.
|
||||
"""
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-wal-regen"
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
conv = repo.create(user, name="regen-test")
|
||||
conv_id = str(conv["id"])
|
||||
|
||||
# Seed five completed messages at positions 0..4.
|
||||
for i in range(5):
|
||||
repo.append_message(
|
||||
conv_id,
|
||||
{
|
||||
"prompt": f"q{i}",
|
||||
"response": f"a{i}",
|
||||
"thought": "",
|
||||
"sources": [],
|
||||
"tool_calls": [],
|
||||
"metadata": {},
|
||||
},
|
||||
)
|
||||
seeded = repo.get_messages(conv_id)
|
||||
assert len(seeded) == 5
|
||||
assert [m["position"] for m in seeded] == [0, 1, 2, 3, 4]
|
||||
|
||||
with _patch_db(pg_conn):
|
||||
result = ConversationService().save_user_question(
|
||||
conversation_id=conv_id,
|
||||
question="q3-regen",
|
||||
decoded_token={"sub": user},
|
||||
index=3,
|
||||
)
|
||||
|
||||
msgs = repo.get_messages(conv_id)
|
||||
# Positions 0,1,2 from the seed plus the new placeholder at 3.
|
||||
assert [m["position"] for m in msgs] == [0, 1, 2, 3]
|
||||
# The placeholder carries the regenerated prompt.
|
||||
regen = next(m for m in msgs if m["position"] == 3)
|
||||
assert regen["prompt"] == "q3-regen"
|
||||
assert regen["status"] == "pending"
|
||||
assert str(regen["id"]) == result["message_id"]
|
||||
# The old answer at index 3 is gone.
|
||||
assert not any(m["response"] == "a3" for m in msgs)
|
||||
# And anything after index 3 was truncated.
|
||||
assert not any(m["prompt"] == "q4" for m in msgs)
|
||||
|
||||
def test_regenerate_at_index_zero_truncates_everything(self, pg_conn):
|
||||
"""``index=0`` is a valid edge: it should drop every prior
|
||||
message and reseat the placeholder at position 0.
|
||||
"""
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-wal-regen-zero"
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
conv = repo.create(user, name="regen-zero")
|
||||
conv_id = str(conv["id"])
|
||||
for i in range(3):
|
||||
repo.append_message(
|
||||
conv_id,
|
||||
{
|
||||
"prompt": f"old-{i}",
|
||||
"response": f"old-a-{i}",
|
||||
"thought": "",
|
||||
"sources": [],
|
||||
"tool_calls": [],
|
||||
"metadata": {},
|
||||
},
|
||||
)
|
||||
|
||||
with _patch_db(pg_conn):
|
||||
ConversationService().save_user_question(
|
||||
conversation_id=conv_id,
|
||||
question="fresh-from-start",
|
||||
decoded_token={"sub": user},
|
||||
index=0,
|
||||
)
|
||||
|
||||
msgs = repo.get_messages(conv_id)
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["position"] == 0
|
||||
assert msgs[0]["prompt"] == "fresh-from-start"
|
||||
|
||||
def test_regenerate_index_ignored_without_conversation_id(self, pg_conn):
|
||||
"""``index`` only makes sense against an existing conversation;
|
||||
the create-then-reserve path silently treats it as a no-op
|
||||
rather than truncating a freshly-created conversation.
|
||||
"""
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-wal-regen-no-conv"
|
||||
with _patch_db(pg_conn):
|
||||
result = ConversationService().save_user_question(
|
||||
conversation_id=None,
|
||||
question="brand new q",
|
||||
decoded_token={"sub": user},
|
||||
index=2,
|
||||
)
|
||||
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
msgs = repo.get_messages(result["conversation_id"])
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["position"] == 0
|
||||
assert msgs[0]["prompt"] == "brand new q"
|
||||
|
||||
def test_raises_when_conversation_unauthorized(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
with _patch_db(pg_conn), pytest.raises(ValueError):
|
||||
ConversationService().save_user_question(
|
||||
conversation_id="00000000-0000-0000-0000-000000000000",
|
||||
question="q",
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
|
||||
|
||||
class TestFinalizeMessage:
|
||||
def test_finalizes_complete(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-fin-ok"
|
||||
with _patch_db(pg_conn):
|
||||
svc = ConversationService()
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="q",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
assert svc.finalize_message(
|
||||
res["message_id"],
|
||||
"real answer",
|
||||
thought="thinking",
|
||||
sources=[{"text": "x" * 2000, "title": "doc"}],
|
||||
tool_calls=[{"name": "search"}],
|
||||
model_id="gpt-4",
|
||||
metadata={"foo": "bar"},
|
||||
status="complete",
|
||||
) is True
|
||||
|
||||
msgs = ConversationsRepository(pg_conn).get_messages(
|
||||
res["conversation_id"],
|
||||
)
|
||||
assert msgs[0]["response"] == "real answer"
|
||||
assert msgs[0]["status"] == "complete"
|
||||
assert msgs[0]["thought"] == "thinking"
|
||||
assert msgs[0]["model_id"] == "gpt-4"
|
||||
# source text trimmed to 1000 chars at finalize time
|
||||
assert len(msgs[0]["sources"][0]["text"]) == 1000
|
||||
assert msgs[0]["metadata"]["foo"] == "bar"
|
||||
|
||||
def test_finalizes_failed_records_error(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-fin-fail"
|
||||
with _patch_db(pg_conn):
|
||||
svc = ConversationService()
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="q",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
err = RuntimeError("provider down")
|
||||
assert svc.finalize_message(
|
||||
res["message_id"],
|
||||
"fallback text",
|
||||
status="failed",
|
||||
error=err,
|
||||
) is True
|
||||
|
||||
msgs = ConversationsRepository(pg_conn).get_messages(
|
||||
res["conversation_id"],
|
||||
)
|
||||
assert msgs[0]["status"] == "failed"
|
||||
assert msgs[0]["metadata"]["error"] == "RuntimeError: provider down"
|
||||
|
||||
def test_finalize_flips_executed_tool_calls(self, pg_conn):
|
||||
"""finalize_message must mark tool_call_attempts.status='executed'
|
||||
rows as 'confirmed' for the same message_id."""
|
||||
from sqlalchemy import text as sql_text
|
||||
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
|
||||
user = "u-fin-tools"
|
||||
with _patch_db(pg_conn):
|
||||
svc = ConversationService()
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="q",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
pg_conn.execute(
|
||||
sql_text(
|
||||
"INSERT INTO tool_call_attempts "
|
||||
"(call_id, message_id, tool_name, action_name, arguments, status) "
|
||||
"VALUES (:cid, CAST(:mid AS uuid), 't', 'a', '{}'::jsonb, 'executed')"
|
||||
),
|
||||
{"cid": "c1", "mid": res["message_id"]},
|
||||
)
|
||||
assert svc.finalize_message(
|
||||
res["message_id"], "ans", status="complete",
|
||||
) is True
|
||||
|
||||
status = pg_conn.execute(
|
||||
sql_text("SELECT status FROM tool_call_attempts WHERE call_id = :cid"),
|
||||
{"cid": "c1"},
|
||||
).scalar()
|
||||
assert status == "confirmed"
|
||||
|
||||
def test_finalize_returns_false_for_unknown_message(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
with _patch_db(pg_conn):
|
||||
assert ConversationService().finalize_message(
|
||||
"00000000-0000-0000-0000-000000000000",
|
||||
"x",
|
||||
status="complete",
|
||||
) is False
|
||||
|
||||
def test_finalize_rolls_back_tool_call_confirm_on_message_update_failure(
|
||||
self, pg_conn
|
||||
):
|
||||
"""Atomicity: if ``update_message_by_id`` raises after the
|
||||
tool_call_attempts confirm ran on the same connection, the
|
||||
confirm rolls back with the rest of the transaction. The
|
||||
``pg_conn`` fixture pins one connection inside an outer
|
||||
rolled-back transaction; we patch ``db_session`` to wrap each
|
||||
call in a SAVEPOINT so the production-code ``with`` block
|
||||
actually rolls back when the message-update raises.
|
||||
"""
|
||||
from contextlib import contextmanager
|
||||
|
||||
from sqlalchemy import text as sql_text
|
||||
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories import conversations as conv_module
|
||||
|
||||
user = "u-fin-rollback"
|
||||
|
||||
@contextmanager
|
||||
def _savepoint_session():
|
||||
nested = pg_conn.begin_nested()
|
||||
try:
|
||||
yield pg_conn
|
||||
nested.commit()
|
||||
except Exception:
|
||||
nested.rollback()
|
||||
raise
|
||||
|
||||
with patch(
|
||||
"application.api.answer.services.conversation_service.db_session",
|
||||
_savepoint_session,
|
||||
), patch(
|
||||
"application.api.answer.services.conversation_service.db_readonly",
|
||||
_savepoint_session,
|
||||
):
|
||||
svc = ConversationService()
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="q",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
pg_conn.execute(
|
||||
sql_text(
|
||||
"INSERT INTO tool_call_attempts "
|
||||
"(call_id, message_id, tool_name, action_name, "
|
||||
"arguments, status) VALUES (:cid, CAST(:mid AS uuid), "
|
||||
"'t', 'a', '{}'::jsonb, 'executed')"
|
||||
),
|
||||
{"cid": "rb-1", "mid": res["message_id"]},
|
||||
)
|
||||
original = conv_module.ConversationsRepository.update_message_by_id
|
||||
|
||||
def boom(self, *args, **kwargs):
|
||||
_ = (args, kwargs)
|
||||
raise RuntimeError("simulated message-update failure")
|
||||
|
||||
conv_module.ConversationsRepository.update_message_by_id = boom
|
||||
try:
|
||||
with pytest.raises(RuntimeError):
|
||||
svc.finalize_message(
|
||||
res["message_id"], "answer", status="complete",
|
||||
)
|
||||
finally:
|
||||
conv_module.ConversationsRepository.update_message_by_id = original
|
||||
|
||||
# The tool_call confirm rolled back: row stays at ``executed``.
|
||||
status = pg_conn.execute(
|
||||
sql_text(
|
||||
"SELECT status FROM tool_call_attempts WHERE call_id = :cid"
|
||||
),
|
||||
{"cid": "rb-1"},
|
||||
).scalar()
|
||||
assert status == "executed"
|
||||
msg_status = pg_conn.execute(
|
||||
sql_text(
|
||||
"SELECT status FROM conversation_messages "
|
||||
"WHERE id = CAST(:mid AS uuid)"
|
||||
),
|
||||
{"mid": res["message_id"]},
|
||||
).scalar()
|
||||
assert msg_status == "pending"
|
||||
|
||||
def test_finalize_generates_title_when_provided(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-fin-title"
|
||||
mock_llm = MagicMock()
|
||||
mock_llm.gen.return_value = "Short Title"
|
||||
with _patch_db(pg_conn):
|
||||
svc = ConversationService()
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="long question that becomes the fallback name",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
assert svc.finalize_message(
|
||||
res["message_id"],
|
||||
"answer",
|
||||
status="complete",
|
||||
title_inputs={
|
||||
"llm": mock_llm,
|
||||
"question": "long question that becomes the fallback name",
|
||||
"response": "answer",
|
||||
"model_id": "gpt-4",
|
||||
"fallback_name": (
|
||||
"long question that becomes the fallback name"[:50]
|
||||
),
|
||||
},
|
||||
) is True
|
||||
|
||||
repo = ConversationsRepository(pg_conn)
|
||||
conv = repo.get_any(res["conversation_id"], user)
|
||||
assert conv["name"] == "Short Title"
|
||||
|
||||
|
||||
class TestSaveUserQuestionFinalizeFailedFlow:
|
||||
"""LLM fails immediately; question stays queryable with status='failed' + error metadata."""
|
||||
|
||||
def test_failed_llm_leaves_question_persisted(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
ConversationService,
|
||||
)
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
user = "u-acceptance"
|
||||
with _patch_db(pg_conn):
|
||||
svc = ConversationService()
|
||||
# Simulates the WAL pre-persist before LLM call.
|
||||
res = svc.save_user_question(
|
||||
conversation_id=None,
|
||||
question="why did this fail?",
|
||||
decoded_token={"sub": user},
|
||||
)
|
||||
# Simulates the LLM raising immediately, caught by complete_stream.
|
||||
try:
|
||||
raise RuntimeError("upstream 503")
|
||||
except RuntimeError as e:
|
||||
svc.finalize_message(
|
||||
res["message_id"],
|
||||
"",
|
||||
status="failed",
|
||||
error=e,
|
||||
)
|
||||
|
||||
msgs = ConversationsRepository(pg_conn).get_messages(
|
||||
res["conversation_id"],
|
||||
)
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["prompt"] == "why did this fail?"
|
||||
assert msgs[0]["status"] == "failed"
|
||||
assert "RuntimeError" in msgs[0]["metadata"]["error"]
|
||||
assert "upstream 503" in msgs[0]["metadata"]["error"]
|
||||
|
||||
|
||||
class TestCompressionMetadata:
|
||||
def test_update_compression_metadata(self, pg_conn):
|
||||
from application.api.answer.services.conversation_service import (
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
"""Token-usage attribution tests for the always-on inline-persist model.
|
||||
|
||||
Persistence is owned by the per-call decorator in ``application.usage``.
|
||||
``finalize_message`` no longer writes ``token_usage`` rows. These tests
|
||||
exercise the decorator path through ``stream_token_usage`` /
|
||||
``gen_token_usage``:
|
||||
|
||||
1. Every LLM call writes one row, regardless of whether the route saves
|
||||
the conversation.
|
||||
2. ``_token_usage_source`` on the LLM instance flows to the row's
|
||||
``source`` column for cost-attribution dashboards.
|
||||
3. ``_request_id`` on the LLM instance flows to the row's ``request_id``
|
||||
column so ``count_in_range`` can DISTINCT-collapse multi-call agent
|
||||
runs into a single request.
|
||||
4. Calls with no attribution (no ``user_id`` and no ``user_api_key``)
|
||||
warn and skip — the repository would otherwise raise on the
|
||||
``token_usage_attribution_chk`` constraint.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _patch_db_session_for(modules, conn):
|
||||
"""Reroute every named module's ``db_session`` to ``conn``."""
|
||||
|
||||
@contextmanager
|
||||
def _yield():
|
||||
yield conn
|
||||
|
||||
patches = [patch(f"{m}.db_session", _yield) for m in modules]
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
|
||||
|
||||
def _seed_user(conn) -> str:
|
||||
user_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
text(
|
||||
"INSERT INTO users (user_id) VALUES (:u) "
|
||||
"ON CONFLICT (user_id) DO NOTHING"
|
||||
),
|
||||
{"u": user_id},
|
||||
)
|
||||
return user_id
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestDecoratorAlwaysPersists:
|
||||
"""Per-call inline persistence — no opt-in flag."""
|
||||
|
||||
def test_primary_stream_writes_agent_stream_row(self, pg_conn):
|
||||
from application.usage import stream_token_usage
|
||||
|
||||
user_id = _seed_user(pg_conn)
|
||||
|
||||
class _PrimaryLLM:
|
||||
decoded_token = {"sub": user_id}
|
||||
user_api_key = None
|
||||
agent_id = None
|
||||
|
||||
def __init__(self):
|
||||
self.token_usage = {"prompt_tokens": 0, "generated_tokens": 0}
|
||||
|
||||
@stream_token_usage
|
||||
def _raw(self, model, messages, stream, tools, **kwargs):
|
||||
yield "chunk-a"
|
||||
yield "chunk-b"
|
||||
|
||||
llm = _PrimaryLLM()
|
||||
with _patch_db_session_for(("application.usage",), pg_conn):
|
||||
for _ in llm._raw(
|
||||
"m", [{"role": "user", "content": "hi"}], True, None,
|
||||
):
|
||||
pass
|
||||
|
||||
row = pg_conn.execute(
|
||||
text(
|
||||
"SELECT prompt_tokens, generated_tokens, source, request_id "
|
||||
"FROM token_usage WHERE user_id = :u"
|
||||
),
|
||||
{"u": user_id},
|
||||
).fetchone()
|
||||
assert row is not None
|
||||
assert row[2] == "agent_stream"
|
||||
assert row[3] is None # No request_id stamped on this LLM.
|
||||
assert row[0] > 0
|
||||
assert row[1] > 0
|
||||
|
||||
def test_side_channel_source_flows_to_row(self, pg_conn):
|
||||
"""``_token_usage_source`` overrides the default ``agent_stream``."""
|
||||
from application.usage import stream_token_usage
|
||||
|
||||
user_id = _seed_user(pg_conn)
|
||||
|
||||
class _RagLLM:
|
||||
decoded_token = {"sub": user_id}
|
||||
user_api_key = None
|
||||
agent_id = None
|
||||
_token_usage_source = "rag_condense"
|
||||
|
||||
def __init__(self):
|
||||
self.token_usage = {"prompt_tokens": 0, "generated_tokens": 0}
|
||||
|
||||
@stream_token_usage
|
||||
def _raw(self, model, messages, stream, tools, **kwargs):
|
||||
yield "chunk"
|
||||
|
||||
llm = _RagLLM()
|
||||
with _patch_db_session_for(("application.usage",), pg_conn):
|
||||
for _ in llm._raw("m", [{"role": "user", "content": "q"}], True, None):
|
||||
pass
|
||||
|
||||
row = pg_conn.execute(
|
||||
text(
|
||||
"SELECT source FROM token_usage WHERE user_id = :u"
|
||||
),
|
||||
{"u": user_id},
|
||||
).fetchone()
|
||||
assert row is not None
|
||||
assert row[0] == "rag_condense"
|
||||
|
||||
def test_request_id_propagates_to_row(self, pg_conn):
|
||||
"""``_request_id`` on the LLM (stamped by the route) lands in
|
||||
``token_usage.request_id`` so ``count_in_range`` can DISTINCT it.
|
||||
"""
|
||||
from application.usage import stream_token_usage
|
||||
|
||||
user_id = _seed_user(pg_conn)
|
||||
request_id = f"req-{uuid.uuid4().hex[:12]}"
|
||||
|
||||
class _PrimaryLLM:
|
||||
decoded_token = {"sub": user_id}
|
||||
user_api_key = None
|
||||
agent_id = None
|
||||
|
||||
def __init__(self):
|
||||
self.token_usage = {"prompt_tokens": 0, "generated_tokens": 0}
|
||||
self._request_id = request_id
|
||||
|
||||
@stream_token_usage
|
||||
def _raw(self, model, messages, stream, tools, **kwargs):
|
||||
yield "chunk"
|
||||
|
||||
llm = _PrimaryLLM()
|
||||
with _patch_db_session_for(("application.usage",), pg_conn):
|
||||
# Call twice — the route invokes the LLM once per tool round.
|
||||
for _ in llm._raw("m", [{"role": "user", "content": "q"}], True, None):
|
||||
pass
|
||||
for _ in llm._raw("m", [{"role": "user", "content": "q2"}], True, None):
|
||||
pass
|
||||
|
||||
rows = pg_conn.execute(
|
||||
text(
|
||||
"SELECT request_id FROM token_usage WHERE user_id = :u"
|
||||
),
|
||||
{"u": user_id},
|
||||
).fetchall()
|
||||
assert len(rows) == 2
|
||||
assert all(r[0] == request_id for r in rows)
|
||||
|
||||
def test_zero_count_call_is_skipped(self, pg_conn):
|
||||
from application.usage import gen_token_usage
|
||||
|
||||
user_id = _seed_user(pg_conn)
|
||||
|
||||
class _EmptyLLM:
|
||||
decoded_token = {"sub": user_id}
|
||||
user_api_key = None
|
||||
agent_id = None
|
||||
|
||||
def __init__(self):
|
||||
self.token_usage = {"prompt_tokens": 0, "generated_tokens": 0}
|
||||
|
||||
@gen_token_usage
|
||||
def _raw(self, model, messages, stream, tools, **kwargs):
|
||||
return None # empty result → 0 generated tokens, 0 prompt tokens
|
||||
|
||||
llm = _EmptyLLM()
|
||||
with _patch_db_session_for(("application.usage",), pg_conn):
|
||||
llm._raw("m", [], False, None)
|
||||
|
||||
n = pg_conn.execute(
|
||||
text("SELECT count(*) FROM token_usage WHERE user_id = :u"),
|
||||
{"u": user_id},
|
||||
).scalar()
|
||||
assert n == 0
|
||||
|
||||
def test_no_attribution_warns_and_skips(self, pg_conn, caplog):
|
||||
"""No user_id and no api_key → log a warning, don't insert.
|
||||
|
||||
The repository would otherwise raise on the attribution CHECK
|
||||
constraint; the decorator skips before that to keep the stream
|
||||
running.
|
||||
"""
|
||||
from application.usage import stream_token_usage
|
||||
|
||||
class _OrphanLLM:
|
||||
decoded_token = None
|
||||
user_api_key = None
|
||||
agent_id = None
|
||||
|
||||
def __init__(self):
|
||||
self.token_usage = {"prompt_tokens": 0, "generated_tokens": 0}
|
||||
|
||||
@stream_token_usage
|
||||
def _raw(self, model, messages, stream, tools, **kwargs):
|
||||
yield "chunk"
|
||||
|
||||
llm = _OrphanLLM()
|
||||
with _patch_db_session_for(
|
||||
("application.usage",), pg_conn,
|
||||
), caplog.at_level(logging.WARNING, logger="application.usage"):
|
||||
for _ in llm._raw("m", [{"role": "user", "content": "q"}], True, None):
|
||||
pass
|
||||
|
||||
n = pg_conn.execute(text("SELECT count(*) FROM token_usage")).scalar()
|
||||
# New attribution rows specifically for this orphan path: nothing
|
||||
# should land. The fixture pins state, so an existing baseline is
|
||||
# 0 by default.
|
||||
assert n == 0
|
||||
assert any(
|
||||
"no user_id/api_key" in r.message
|
||||
for r in caplog.records
|
||||
)
|
||||
@@ -11,7 +11,8 @@ Additional coverage beyond tests/api/answer/routes/test_base.py:
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from unittest.mock import MagicMock
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -368,6 +369,11 @@ class TestCompleteStreamCompressionMetadata:
|
||||
|
||||
resource.conversation_service = MagicMock()
|
||||
resource.conversation_service.save_conversation.return_value = "conv123"
|
||||
resource.conversation_service.save_user_question.return_value = {
|
||||
"conversation_id": "conv123",
|
||||
"message_id": "msg123",
|
||||
"request_id": "req123",
|
||||
}
|
||||
|
||||
stream = list(
|
||||
resource.complete_stream(
|
||||
@@ -404,6 +410,11 @@ class TestCompleteStreamCompressionMetadata:
|
||||
|
||||
resource.conversation_service = MagicMock()
|
||||
resource.conversation_service.save_conversation.return_value = "conv123"
|
||||
resource.conversation_service.save_user_question.return_value = {
|
||||
"conversation_id": "conv123",
|
||||
"message_id": "msg123",
|
||||
"request_id": "req123",
|
||||
}
|
||||
resource.conversation_service.update_compression_metadata.side_effect = (
|
||||
Exception("db error")
|
||||
)
|
||||
@@ -478,6 +489,11 @@ class TestCompleteStreamGeneratorExit:
|
||||
|
||||
resource.conversation_service = MagicMock()
|
||||
resource.conversation_service.save_conversation.return_value = "conv1"
|
||||
resource.conversation_service.save_user_question.return_value = {
|
||||
"conversation_id": "conv1",
|
||||
"message_id": "msg1",
|
||||
"request_id": "req1",
|
||||
}
|
||||
|
||||
gen = resource.complete_stream(
|
||||
question="Q",
|
||||
@@ -489,7 +505,9 @@ class TestCompleteStreamGeneratorExit:
|
||||
model_id="gpt-4",
|
||||
)
|
||||
|
||||
# Read first chunk and then close (simulating client disconnect)
|
||||
# Drain the early ``message_id`` event before reading the
|
||||
# ``partial`` chunk that this test is asserting on.
|
||||
next(gen)
|
||||
chunk = next(gen)
|
||||
assert "partial" in chunk
|
||||
gen.close() # This triggers GeneratorExit
|
||||
@@ -512,6 +530,11 @@ class TestCompleteStreamGeneratorExit:
|
||||
|
||||
resource.conversation_service = MagicMock()
|
||||
resource.conversation_service.save_conversation.return_value = "conv1"
|
||||
resource.conversation_service.save_user_question.return_value = {
|
||||
"conversation_id": "conv1",
|
||||
"message_id": "msg1",
|
||||
"request_id": "req1",
|
||||
}
|
||||
|
||||
gen = resource.complete_stream(
|
||||
question="Q",
|
||||
@@ -524,6 +547,8 @@ class TestCompleteStreamGeneratorExit:
|
||||
isNoneDoc=True,
|
||||
)
|
||||
|
||||
# Skip past the early ``message_id`` event.
|
||||
next(gen)
|
||||
next(gen)
|
||||
gen.close()
|
||||
|
||||
@@ -547,6 +572,11 @@ class TestCompleteStreamGeneratorExit:
|
||||
resource.conversation_service.save_conversation.side_effect = Exception(
|
||||
"save error"
|
||||
)
|
||||
resource.conversation_service.save_user_question.return_value = {
|
||||
"conversation_id": "conv1",
|
||||
"message_id": "msg1",
|
||||
"request_id": "req1",
|
||||
}
|
||||
|
||||
gen = resource.complete_stream(
|
||||
question="Q",
|
||||
@@ -558,5 +588,127 @@ class TestCompleteStreamGeneratorExit:
|
||||
model_id="gpt-4",
|
||||
)
|
||||
|
||||
# Skip past the early ``message_id`` event.
|
||||
next(gen)
|
||||
next(gen)
|
||||
gen.close() # Should not crash even with save error
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _patch_db_session(conn):
|
||||
@contextmanager
|
||||
def _yield():
|
||||
yield conn
|
||||
|
||||
with patch(
|
||||
"application.api.answer.services.conversation_service.db_session",
|
||||
_yield,
|
||||
), patch(
|
||||
"application.api.answer.services.conversation_service.db_readonly",
|
||||
_yield,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCompleteStreamWalAcceptance:
|
||||
"""Acceptance for the WAL pre-persist behaviour: when the LLM raises
|
||||
immediately, the user question is still queryable from PG with
|
||||
status='failed' and a meaningful error in metadata."""
|
||||
|
||||
def test_failed_llm_persists_question_with_failed_status(
|
||||
self, pg_conn, flask_app,
|
||||
):
|
||||
from application.api.answer.routes.base import BaseAnswerResource
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
with flask_app.app_context():
|
||||
resource = BaseAnswerResource()
|
||||
|
||||
mock_agent = MagicMock()
|
||||
mock_agent.gen.side_effect = RuntimeError("LLM upstream failed")
|
||||
|
||||
with _patch_db_session(pg_conn):
|
||||
stream = list(
|
||||
resource.complete_stream(
|
||||
question="why does the WAL matter?",
|
||||
agent=mock_agent,
|
||||
conversation_id=None,
|
||||
user_api_key=None,
|
||||
decoded_token={"sub": "u-acceptance"},
|
||||
should_save_conversation=True,
|
||||
model_id="gpt-4",
|
||||
)
|
||||
)
|
||||
error_chunks = [s for s in stream if '"type": "error"' in s]
|
||||
assert len(error_chunks) == 1
|
||||
|
||||
from sqlalchemy import text as sql_text
|
||||
convs = pg_conn.execute(
|
||||
sql_text("SELECT id FROM conversations WHERE user_id = :u"),
|
||||
{"u": "u-acceptance"},
|
||||
).fetchall()
|
||||
assert len(convs) == 1
|
||||
conv_id = str(convs[0][0])
|
||||
msgs = ConversationsRepository(pg_conn).get_messages(conv_id)
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["prompt"] == "why does the WAL matter?"
|
||||
assert msgs[0]["status"] == "failed"
|
||||
assert "RuntimeError" in msgs[0]["metadata"]["error"]
|
||||
assert "LLM upstream failed" in msgs[0]["metadata"]["error"]
|
||||
|
||||
def test_request_id_consistent_across_sse_event_and_wal_row(
|
||||
self, pg_conn, flask_app,
|
||||
):
|
||||
"""The early ``message_id`` SSE event reports the same
|
||||
``request_id`` that ``save_user_question`` writes onto the WAL
|
||||
row, so client-side correlation, ``token_usage`` joins, and
|
||||
``count_in_range``'s DISTINCT all line up.
|
||||
"""
|
||||
from application.api.answer.routes.base import BaseAnswerResource
|
||||
from application.storage.db.repositories.conversations import (
|
||||
ConversationsRepository,
|
||||
)
|
||||
|
||||
with flask_app.app_context():
|
||||
resource = BaseAnswerResource()
|
||||
mock_agent = MagicMock()
|
||||
mock_agent.gen.return_value = iter([{"answer": "ok"}])
|
||||
mock_agent.tool_calls = []
|
||||
mock_agent.compression_metadata = None
|
||||
mock_agent.compression_saved = False
|
||||
|
||||
with _patch_db_session(pg_conn):
|
||||
stream = list(
|
||||
resource.complete_stream(
|
||||
question="hello",
|
||||
agent=mock_agent,
|
||||
conversation_id=None,
|
||||
user_api_key=None,
|
||||
decoded_token={"sub": "u-request-id"},
|
||||
should_save_conversation=True,
|
||||
model_id="gpt-4",
|
||||
)
|
||||
)
|
||||
|
||||
sse_events = [
|
||||
json.loads(s.replace("data: ", "").strip())
|
||||
for s in stream
|
||||
if s.startswith("data: ")
|
||||
]
|
||||
early_events = [e for e in sse_events if e.get("type") == "message_id"]
|
||||
assert len(early_events) == 1
|
||||
sse_request_id = early_events[0]["request_id"]
|
||||
assert sse_request_id
|
||||
|
||||
from sqlalchemy import text as sql_text
|
||||
convs = pg_conn.execute(
|
||||
sql_text("SELECT id FROM conversations WHERE user_id = :u"),
|
||||
{"u": "u-request-id"},
|
||||
).fetchall()
|
||||
assert len(convs) == 1
|
||||
msgs = ConversationsRepository(pg_conn).get_messages(str(convs[0][0]))
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0]["request_id"] == sse_request_id
|
||||
Reference in new issue
Block a user