mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 05:49:42 +08:00
fix(session): preserve opaque PocketBase message cursors
This commit is contained in:
@@ -528,19 +528,25 @@ class ContextBuilder:
|
||||
recent_budget = self._recent_budget(budget)
|
||||
|
||||
stored_summary = str(session.get("compressed_summary", "") or "").strip()
|
||||
summary_up_to_msg_id = int(session.get("summary_up_to_msg_id", 0) or 0)
|
||||
summary_up_to_msg_id = session.get("summary_up_to_msg_id", 0) or 0
|
||||
# Branch guard: the watermark must sit on this turn's ancestor chain.
|
||||
# After an edit-branch switch it may point into a sibling branch — the
|
||||
# stored summary would then carry content this branch never saw.
|
||||
# Discard both and rebuild from this branch's own messages.
|
||||
if summary_up_to_msg_id > 0 and not any(
|
||||
int(item.get("id", 0) or 0) == summary_up_to_msg_id for item in messages
|
||||
):
|
||||
summary_position = next(
|
||||
(
|
||||
index
|
||||
for index, item in enumerate(messages)
|
||||
if summary_up_to_msg_id and str(item.get("id")) == str(summary_up_to_msg_id)
|
||||
),
|
||||
None,
|
||||
)
|
||||
if summary_up_to_msg_id and summary_position is None:
|
||||
stored_summary = ""
|
||||
summary_up_to_msg_id = 0
|
||||
unsummarized = [
|
||||
item for item in messages if int(item.get("id", 0) or 0) > summary_up_to_msg_id
|
||||
]
|
||||
unsummarized = (
|
||||
messages[summary_position + 1 :] if summary_position is not None else messages
|
||||
)
|
||||
|
||||
current_history = self._build_history(stored_summary, unsummarized)
|
||||
current_tokens = self._model_tokens(unsummarized, stored_summary)
|
||||
@@ -629,7 +635,7 @@ class ContextBuilder:
|
||||
# past turns that were not actually folded in.
|
||||
up_to_msg_id = summary_up_to_msg_id
|
||||
if prefix_messages:
|
||||
up_to_msg_id = max(summary_up_to_msg_id, int(prefix_messages[-1].get("id", 0) or 0))
|
||||
up_to_msg_id = prefix_messages[-1].get("id", 0) or 0
|
||||
await self.store.update_summary(session_id, new_summary, up_to_msg_id)
|
||||
stored_summary = new_summary
|
||||
retained_rows = recent_messages
|
||||
|
||||
@@ -45,6 +45,7 @@ _ALL_TURN_STATUSES = _ACTIVE_TURN_STATUSES | _TERMINAL_TURN_STATUSES
|
||||
# The deployed PocketBase messages collection has no parent column. Keep the
|
||||
# link inside its JSON metadata until the collection schema can be upgraded.
|
||||
_PARENT_MESSAGE_METADATA_KEY = "_parent_message_id"
|
||||
_SUMMARY_CURSOR_PREFERENCE_KEY = "_summary_message_id"
|
||||
|
||||
|
||||
def _captured_store_context(method):
|
||||
@@ -320,6 +321,9 @@ class PocketBaseSessionStore:
|
||||
or time.time()
|
||||
)
|
||||
preferences_raw = getattr(record, "preferences_json", None)
|
||||
preferences = _json_loads(preferences_raw, {})
|
||||
preferences = dict(preferences) if isinstance(preferences, dict) else {}
|
||||
summary_cursor = preferences.pop(_SUMMARY_CURSOR_PREFERENCE_KEY, None)
|
||||
deleted_at_raw = getattr(record, "deleted_at", None)
|
||||
return {
|
||||
"id": sid,
|
||||
@@ -328,11 +332,12 @@ class PocketBaseSessionStore:
|
||||
"created_at": created,
|
||||
"updated_at": updated,
|
||||
"compressed_summary": getattr(record, "compressed_summary", "") or "",
|
||||
"summary_up_to_msg_id": int(getattr(record, "summary_up_to_msg_id", 0) or 0),
|
||||
"summary_up_to_msg_id": summary_cursor
|
||||
or int(getattr(record, "summary_up_to_msg_id", 0) or 0),
|
||||
# PocketBase has no local schema-upgrade hook. Normalize at the
|
||||
# repository boundary so old remote rows immediately satisfy the
|
||||
# same API contract; their next preference write persists it.
|
||||
"preferences": upgrade_workspace_preferences(_json_loads(preferences_raw, {})),
|
||||
"preferences": upgrade_workspace_preferences(preferences),
|
||||
"capability": getattr(record, "capability", "") or "",
|
||||
"status": getattr(record, "status", "idle") or "idle",
|
||||
"active_turn_id": "",
|
||||
@@ -777,7 +782,7 @@ class PocketBaseSessionStore:
|
||||
return {"message_count": 0, "last_message": ""}
|
||||
|
||||
@_captured_store_context
|
||||
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int) -> bool:
|
||||
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int | str) -> bool:
|
||||
sid = _validate_id(session_id, "session_id")
|
||||
uid = _current_user_id()
|
||||
|
||||
@@ -785,11 +790,22 @@ class PocketBaseSessionStore:
|
||||
record = _find_session_record(_pb(), sid, uid)
|
||||
if record is None:
|
||||
return False
|
||||
preferences = _json_loads(getattr(record, "preferences_json", None), {})
|
||||
preferences = dict(preferences) if isinstance(preferences, dict) else {}
|
||||
# The existing server field is numeric. Keep native record IDs in
|
||||
# its JSON preferences field, hidden at the repository boundary.
|
||||
if isinstance(up_to_msg_id, str):
|
||||
preferences[_SUMMARY_CURSOR_PREFERENCE_KEY] = up_to_msg_id
|
||||
else:
|
||||
preferences.pop(_SUMMARY_CURSOR_PREFERENCE_KEY, None)
|
||||
_pb().collection("sessions").update(
|
||||
record.id,
|
||||
{
|
||||
"compressed_summary": summary,
|
||||
"summary_up_to_msg_id": max(0, int(up_to_msg_id)),
|
||||
"summary_up_to_msg_id": 0
|
||||
if isinstance(up_to_msg_id, str)
|
||||
else max(0, up_to_msg_id),
|
||||
"preferences_json": preferences,
|
||||
},
|
||||
)
|
||||
return True
|
||||
@@ -819,6 +835,9 @@ class PocketBaseSessionStore:
|
||||
record = _find_session_record(_pb(), sid, uid)
|
||||
if record is None:
|
||||
return False
|
||||
current = _json_loads(getattr(record, "preferences_json", None), {})
|
||||
if isinstance(current, dict) and _SUMMARY_CURSOR_PREFERENCE_KEY in current:
|
||||
merged[_SUMMARY_CURSOR_PREFERENCE_KEY] = current[_SUMMARY_CURSOR_PREFERENCE_KEY]
|
||||
_pb().collection("sessions").update(
|
||||
record.id,
|
||||
{"preferences_json": merged, "session_updated_at": time.time()},
|
||||
|
||||
@@ -247,7 +247,9 @@ class SessionStoreProtocol(SessionRepository, TurnRepository, MessageRepository,
|
||||
session_ids: list[str],
|
||||
) -> list[dict[str, Any]]: ...
|
||||
|
||||
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int) -> bool: ...
|
||||
async def update_summary(
|
||||
self, session_id: str, summary: str, up_to_msg_id: int | str
|
||||
) -> bool: ...
|
||||
|
||||
async def update_session_preferences(
|
||||
self, session_id: str, preferences: dict[str, Any]
|
||||
|
||||
@@ -117,7 +117,7 @@ async def build_inventory(
|
||||
store: SessionStoreProtocol,
|
||||
*,
|
||||
session_id: str,
|
||||
leaf_message_id: int | None,
|
||||
leaf_message_id: int | str | None,
|
||||
current_turn_ordinal: int,
|
||||
fresh_attachment_records: Sequence[dict[str, Any]],
|
||||
fresh_notebook_records: Sequence[dict[str, Any]],
|
||||
@@ -448,7 +448,7 @@ async def _add_historical(
|
||||
*,
|
||||
store: SessionStoreProtocol,
|
||||
session_id: str,
|
||||
leaf_message_id: int | None,
|
||||
leaf_message_id: int | str | None,
|
||||
language: str = "en",
|
||||
) -> None:
|
||||
"""Walk the active branch's ancestor user messages and pull in
|
||||
@@ -663,7 +663,7 @@ async def _collect_from_user_message(
|
||||
async def _load_lineage(
|
||||
store: SessionStoreProtocol,
|
||||
session_id: str,
|
||||
leaf_message_id: int | None,
|
||||
leaf_message_id: int | str | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Return the active branch's ancestor user/assistant messages in
|
||||
chronological order. When ``leaf_message_id`` is ``None`` (legacy
|
||||
@@ -676,21 +676,21 @@ async def _load_lineage(
|
||||
all_msgs = await store.get_messages(session_id)
|
||||
if leaf_message_id is None:
|
||||
return all_msgs
|
||||
by_id: dict[int, dict[str, Any]] = {}
|
||||
by_id: dict[str, dict[str, Any]] = {}
|
||||
for m in all_msgs:
|
||||
mid = m.get("id")
|
||||
if mid is not None:
|
||||
by_id[int(mid)] = m
|
||||
by_id[str(mid)] = m
|
||||
chain: list[dict[str, Any]] = []
|
||||
current: int | None = int(leaf_message_id)
|
||||
current: str | None = str(leaf_message_id)
|
||||
safety = 10_000
|
||||
while current is not None and safety > 0:
|
||||
m = by_id.get(int(current))
|
||||
m = by_id.get(current)
|
||||
if m is None:
|
||||
break
|
||||
chain.append(m)
|
||||
parent = m.get("parent_message_id")
|
||||
current = int(parent) if parent is not None else None
|
||||
current = str(parent) if parent is not None else None
|
||||
safety -= 1
|
||||
chain.reverse()
|
||||
return chain
|
||||
@@ -713,7 +713,7 @@ async def collect_prior_image_attachments(
|
||||
store: SessionStoreProtocol,
|
||||
*,
|
||||
session_id: str,
|
||||
leaf_message_id: int | None,
|
||||
leaf_message_id: int | str | None,
|
||||
exclude_urls: set[str] | None = None,
|
||||
limit: int | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
|
||||
@@ -3077,8 +3077,8 @@ class SQLiteSessionStore:
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int) -> bool:
|
||||
return await self._run(self._update_summary_sync, session_id, summary, up_to_msg_id)
|
||||
async def update_summary(self, session_id: str, summary: str, up_to_msg_id: int | str) -> bool:
|
||||
return await self._run(self._update_summary_sync, session_id, summary, int(up_to_msg_id))
|
||||
|
||||
def _update_session_preferences_sync(
|
||||
self, session_id: str, preferences: dict[str, Any]
|
||||
|
||||
@@ -147,6 +147,87 @@ async def test_create_session_stamps_current_user(fake_pb) -> None:
|
||||
assert row.session_id == "s_alice"
|
||||
|
||||
|
||||
async def test_context_builder_accepts_pocketbase_message_ids(fake_pb) -> None:
|
||||
"""The second turn must retain native string-ID history without coercion."""
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deeptutor.services.session.context_builder import ContextBuilder
|
||||
|
||||
store = PocketBaseSessionStore()
|
||||
with as_user("alice"):
|
||||
session = await store.create_session(session_id="s_context")
|
||||
await store.add_message(session["id"], "user", "first question")
|
||||
await store.add_message(session["id"], "assistant", "first answer")
|
||||
result = await ContextBuilder(store).build(
|
||||
session_id=session["id"],
|
||||
llm_config=SimpleNamespace(
|
||||
max_tokens=4096, model="test-model", context_window=32768, binding=None
|
||||
),
|
||||
)
|
||||
assert [row["content"] for row in result.conversation_history] == [
|
||||
"first question",
|
||||
"first answer",
|
||||
]
|
||||
|
||||
|
||||
async def test_pocketbase_summary_preserves_native_message_cursor(fake_pb) -> None:
|
||||
"""Store native cursors without changing the existing numeric server schema."""
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deeptutor.services.session.context_builder import ContextBuilder
|
||||
|
||||
store = PocketBaseSessionStore()
|
||||
with as_user("alice"):
|
||||
session = await store.create_session(session_id="s_cursor")
|
||||
await store.add_message(session["id"], "user", "summarized")
|
||||
first = fake_pb.collection("messages").get_full_list()[0]
|
||||
first.id = "zzzzzzzzzzzzzzz"
|
||||
message_id = first.id
|
||||
assert await store.update_summary(session["id"], "summary", message_id)
|
||||
loaded = await store.get_session(session["id"])
|
||||
assert loaded["summary_up_to_msg_id"] == message_id
|
||||
assert loaded["compressed_summary"] == "summary"
|
||||
await store.update_session_preferences(session["id"], {"language": "en"})
|
||||
assert (await store.get_session(session["id"]))["summary_up_to_msg_id"] == message_id
|
||||
await store.add_message(session["id"], "user", "later question")
|
||||
fake_pb.collection("messages").get_full_list()[-1].id = "aaaaaaaaaaaaaaa"
|
||||
result = await ContextBuilder(store).build(
|
||||
session_id=session["id"],
|
||||
llm_config=SimpleNamespace(
|
||||
max_tokens=4096, model="test-model", context_window=32768, binding=None
|
||||
),
|
||||
)
|
||||
assert [row["content"] for row in result.conversation_history] == [
|
||||
"summary",
|
||||
"later question",
|
||||
]
|
||||
assert "_summary_message_id" not in loaded["preferences"]
|
||||
assert await store.update_summary(session["id"], "", 0)
|
||||
assert (await store.get_session(session["id"]))["summary_up_to_msg_id"] == 0
|
||||
|
||||
|
||||
async def test_source_inventory_accepts_native_parent_ids(fake_pb) -> None:
|
||||
"""The lineage walker must retain attachments when native IDs are strings."""
|
||||
from deeptutor.services.session.source_inventory import collect_prior_image_attachments
|
||||
|
||||
store = PocketBaseSessionStore()
|
||||
with as_user("alice"):
|
||||
session = await store.create_session(session_id="s_lineage")
|
||||
user_id = await store.add_message(
|
||||
session["id"],
|
||||
"user",
|
||||
"image",
|
||||
attachments=[{"id": "image", "mime_type": "image/png", "url": "/image.png"}],
|
||||
)
|
||||
leaf_id = await store.add_message(
|
||||
session["id"], "assistant", "answer", parent_message_id=user_id
|
||||
)
|
||||
images = await collect_prior_image_attachments(
|
||||
store, session_id=session["id"], leaf_message_id=leaf_id, limit=1
|
||||
)
|
||||
assert [row["url"] for row in images] == ["/image.png"]
|
||||
|
||||
|
||||
async def test_list_sessions_only_returns_own(fake_pb) -> None:
|
||||
store = PocketBaseSessionStore()
|
||||
with as_user("alice"):
|
||||
|
||||
@@ -167,7 +167,7 @@ export interface SessionDetail {
|
||||
"idle" | "running" | "completed" | "failed" | "cancelled" | "rejected";
|
||||
active_turn_id?: string;
|
||||
compressed_summary?: string;
|
||||
summary_up_to_msg_id?: number;
|
||||
summary_up_to_msg_id?: number | string;
|
||||
preferences?: SessionPreferences;
|
||||
messages: SessionMessage[];
|
||||
active_turns?: ActiveTurnSummary[];
|
||||
|
||||
Reference in New Issue
Block a user