Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(chat): keep parked ask_user turns alive across deletion and reload (#1789)

Wenjun Lifdebb2761ec0
diff --git a/deeptutor/api/routers/sessions.py b/deeptutor/api/routers/sessions.py
index 310c86d40..3f75c8e4a 100644
--- a/deeptutor/api/routers/sessions.py
+++ b/deeptutor/api/routers/sessions.py
@@ -554,9 +554,10 @@ async def update_branch_selection(session_id: str, payload: BranchSelectionReque
async def delete_turn_by_message(session_id: str, message_id: int):
store = get_sqlite_session_store()
result = await store.delete_turn_by_message(session_id, message_id)
- if result["was_running"]:
+ if result["was_active"]:
raise HTTPException(
- status_code=409, detail="Cannot delete a message while its turn is running"
+ status_code=409,
+ detail="Cannot delete a message while its turn is running or waiting for input",
)
if not result["deleted"]:
raise HTTPException(status_code=404, detail="Message not found")
diff --git a/deeptutor/services/session/sqlite_store.py b/deeptutor/services/session/sqlite_store.py
index 955e40248..bf2ca4e8e 100644
--- a/deeptutor/services/session/sqlite_store.py
+++ b/deeptutor/services/session/sqlite_store.py
@@ -2139,7 +2139,7 @@ class SQLiteSessionStore:
with self._connect() as conn:
msg = conn.execute(
"""
- SELECT id, session_id, role, attachments_json, created_at
+ SELECT id, session_id, role, attachments_json, metadata_json, created_at
FROM messages
WHERE id = ?
""",
@@ -2150,7 +2150,7 @@ class SQLiteSessionStore:
"deleted": False,
"attachment_ids": [],
"turn_id": None,
- "was_running": False,
+ "was_active": False,
}
role = msg["role"]
@@ -2158,7 +2158,7 @@ class SQLiteSessionStore:
if role == "user":
paired_msg = conn.execute(
"""
- SELECT id, session_id, role, attachments_json, created_at
+ SELECT id, session_id, role, attachments_json, metadata_json, created_at
FROM messages
WHERE session_id = ? AND role = 'assistant' AND id > ?
ORDER BY id ASC
@@ -2169,7 +2169,7 @@ class SQLiteSessionStore:
elif role == "assistant":
paired_msg = conn.execute(
"""
- SELECT id, session_id, role, attachments_json, created_at
+ SELECT id, session_id, role, attachments_json, metadata_json, created_at
FROM messages
WHERE session_id = ? AND role = 'user' AND id < ?
ORDER BY id DESC
@@ -2179,30 +2179,67 @@ class SQLiteSessionStore:
).fetchone()
user_msg = msg if role == "user" else paired_msg
- turn_id = None
- was_running = False
- if user_msg is not None:
- user_created_at = user_msg["created_at"]
+ turn_row = None
+ if role == "assistant":
+ # An assistant row is linked to the turn that produced it,
+ # which stays authoritative when ordering alone cannot tell
+ # (a regenerated answer shares its user row with the old turn).
+ turn_row = conn.execute(
+ """
+ SELECT id, status
+ FROM turns
+ WHERE session_id = ? AND assistant_message_id = ?
+ LIMIT 1
+ """,
+ (session_id, int(message_id)),
+ ).fetchone()
+
+ if turn_row is None and user_msg is not None:
+ # Production stamps the owning turn id on the user row
+ # (executor writes metadata.turn_id), including the recovered
+ # worker_lost case that never gets an assistant row.
+ stamped_turn_id = ""
+ metadata = _json_loads(user_msg["metadata_json"], {})
+ if isinstance(metadata, dict):
+ stamped_turn_id = str(metadata.get("turn_id") or "").strip()
+ if stamped_turn_id:
+ turn_row = conn.execute(
+ """
+ SELECT id, status
+ FROM turns
+ WHERE session_id = ? AND id = ?
+ LIMIT 1
+ """,
+ (session_id, stamped_turn_id),
+ ).fetchone()
+
+ if turn_row is None and user_msg is not None:
+ # Legacy rows predate the stamp: the owning turn is the last
+ # one created at or before the user row. A turn row is always
+ # written before its user row, so a later turn must never match.
turn_row = conn.execute(
"""
SELECT id, status
FROM turns
- WHERE session_id = ? AND created_at >= ?
- ORDER BY created_at ASC
+ WHERE session_id = ? AND created_at <= ?
+ ORDER BY created_at DESC
LIMIT 1
""",
- (session_id, user_created_at),
+ (session_id, user_msg["created_at"]),
).fetchone()
- if turn_row is not None:
- turn_id = turn_row["id"]
- was_running = turn_row["status"] == "running"
- if was_running:
+ turn_id = None
+ was_active = False
+ if turn_row is not None:
+ turn_id = turn_row["id"]
+ was_active = turn_row["status"] in ACTIVE_TURN_STATUSES
+
+ if was_active:
return {
"deleted": False,
"attachment_ids": [],
"turn_id": turn_id,
- "was_running": True,
+ "was_active": True,
}
attachment_ids: list[str] = []
@@ -2264,7 +2301,7 @@ class SQLiteSessionStore:
"deleted": True,
"attachment_ids": attachment_ids,
"turn_id": turn_id,
- "was_running": was_running,
+ "was_active": was_active,
}
async def delete_turn_by_message(self, session_id: str, message_id: int) -> dict[str, Any]:
diff --git a/tests/services/session/test_sqlite_store.py b/tests/services/session/test_sqlite_store.py
index 546047222..245ae5af6 100644
--- a/tests/services/session/test_sqlite_store.py
+++ b/tests/services/session/test_sqlite_store.py
@@ -934,6 +934,108 @@ def test_delete_last_turn_leaves_prefix_intact(store: SQLiteSessionStore) -> Non
assert remaining[1]["id"] == a1
+def _seed_turns_with_parked_second(
+ store: SQLiteSessionStore, *, stamp: bool = True
+) -> dict[str, object]:
+ """Seed the real lifecycle order: turn row first, then its user row.
+
+ Turn 1 is completed; turn 2 is parked on ``waiting_input`` with no
+ assistant row. ``stamp=False`` simulates legacy rows persisted before
+ ``metadata.turn_id`` existed; production writes it (executor.py).
+ """
+ sid = asyncio.run(store.create_session())["id"]
+ t1 = asyncio.run(store.begin_turn(sid, capability="chat"))
+ u1 = asyncio.run(
+ store.add_message(sid, "user", "q1", metadata={"turn_id": t1["id"]} if stamp else None)
+ )
+ a1 = asyncio.run(store.add_message(sid, "assistant", "a1", parent_message_id=u1))
+ asyncio.run(store.link_turn_message(t1["id"], a1))
+ asyncio.run(store.transition_turn(t1["id"], "completed", expected_status="running"))
+ t2 = asyncio.run(store.begin_turn(sid, capability="chat"))
+ u2 = asyncio.run(
+ store.add_message(sid, "user", "q2", metadata={"turn_id": t2["id"]} if stamp else None)
+ )
+ asyncio.run(store.transition_turn(t2["id"], "waiting_input", expected_status="running"))
+ return {"sid": sid, "t1": t1["id"], "u1": u1, "a1": a1, "t2": t2["id"], "u2": u2}
+
+
+def test_delete_completed_turn_message_leaves_waiting_turn_intact(
+ store: SQLiteSessionStore,
+) -> None:
+ ids = _seed_turns_with_parked_second(store)
+
+ result = asyncio.run(store.delete_turn_by_message(str(ids["sid"]), int(ids["u1"])))
+
+ assert result["deleted"] is True
+ assert result["turn_id"] == ids["t1"]
+ assert asyncio.run(store.get_turn(str(ids["t1"]))) is None
+ parked = asyncio.run(store.get_turn(str(ids["t2"])))
+ assert parked is not None
+ assert parked["status"] == "waiting_input"
+ remaining = asyncio.run(store.get_messages(str(ids["sid"])))
+ assert [m["content"] for m in remaining] == ["q2"]
+
+
+def test_delete_message_of_active_turn_is_refused(store: SQLiteSessionStore) -> None:
+ ids = _seed_turns_with_parked_second(store)
+
+ result = asyncio.run(store.delete_turn_by_message(str(ids["sid"]), int(ids["u2"])))
+
+ assert result["deleted"] is False
+ assert result["was_active"] is True
+ assert result["turn_id"] == ids["t2"]
+ parked = asyncio.run(store.get_turn(str(ids["t2"])))
+ assert parked is not None
+ assert parked["status"] == "waiting_input"
+ remaining = asyncio.run(store.get_messages(str(ids["sid"])))
+ assert [m["content"] for m in remaining] == ["q1", "a1", "q2"]
+
+
+def test_delete_regenerated_assistant_message_resolves_its_own_turn(
+ store: SQLiteSessionStore,
+) -> None:
+ # Regeneration writes a new turn row and assistant row after the original
+ # user row; a completed intermediate turn also exists, so ordering alone
+ # would attribute the assistant row to the wrong turn. Its own turn link
+ # must win.
+ sid = asyncio.run(store.create_session())["id"]
+ t1 = asyncio.run(store.begin_turn(sid, capability="chat"))
+ u1 = asyncio.run(store.add_message(sid, "user", "q1", metadata={"turn_id": t1["id"]}))
+ a1 = asyncio.run(store.add_message(sid, "assistant", "a1", parent_message_id=u1))
+ asyncio.run(store.link_turn_message(t1["id"], a1))
+ asyncio.run(store.transition_turn(t1["id"], "completed", expected_status="running"))
+ t2 = asyncio.run(store.begin_turn(sid, capability="chat"))
+ u2 = asyncio.run(store.add_message(sid, "user", "q2", metadata={"turn_id": t2["id"]}))
+ a2 = asyncio.run(store.add_message(sid, "assistant", "a2", parent_message_id=u2))
+ asyncio.run(store.link_turn_message(t2["id"], a2))
+ asyncio.run(store.transition_turn(t2["id"], "completed", expected_status="running"))
+ t3 = asyncio.run(store.begin_turn(sid, capability="chat"))
+ a3 = asyncio.run(store.add_message(sid, "assistant", "a1-regen", parent_message_id=u1))
+ asyncio.run(store.link_turn_message(t3["id"], a3))
+ asyncio.run(store.transition_turn(t3["id"], "completed", expected_status="running"))
+
+ result = asyncio.run(store.delete_turn_by_message(sid, a3))
+
+ assert result["deleted"] is True
+ assert result["turn_id"] == t3["id"]
+ assert asyncio.run(store.get_turn(t1["id"])) is not None
+ assert asyncio.run(store.get_turn(t2["id"])) is not None
+ assert asyncio.run(store.get_turn(t3["id"])) is None
+
+
+def test_delete_legacy_message_uses_ordering_fallback(store: SQLiteSessionStore) -> None:
+ ids = _seed_turns_with_parked_second(store, stamp=False)
+
+ result = asyncio.run(store.delete_turn_by_message(str(ids["sid"]), int(ids["u1"])))
+
+ assert result["deleted"] is True
+ assert result["turn_id"] == ids["t1"]
+ assert asyncio.run(store.get_turn(str(ids["t1"]))) is None
+ parked = asyncio.run(store.get_turn(str(ids["t2"])))
+ assert parked is not None
+ assert parked["status"] == "waiting_input"
+
+
# ── Context messages ──────────────────────────────────────────────
diff --git a/web/features/chat/ChatStateAdapter.tsx b/web/features/chat/ChatStateAdapter.tsx
index d25840b3e..2f5359402 100644
--- a/web/features/chat/ChatStateAdapter.tsx
+++ b/web/features/chat/ChatStateAdapter.tsx
@@ -1816,6 +1816,12 @@ export function ChatStateAdapterProvider({
Map<string, { storageKey: string; sourceStorageKey?: string; submissionId: string; visibleSubmissionIds: string[] }>
>(new Map());
const resolvingResendRef = useRef<Set<string>>(new Set());
+ // Parked (``waiting_input``) turns already resubscribed to on load, keyed
+ // ``<sessionKey>:<turnId>``. A parked turn keeps the conversation's lease
+ // while it waits, and its ask_user card exists only in the live event
+ // stream (no assistant row is persisted until the turn settles), so opening
+ // the conversation must replay that stream or the card is unreachable.
+ const resumedParkedTurnsRef = useRef<Set<string>>(new Set());
const traceCacheRef = useRef<TraceCache>(new TraceCache());
const traceRequestsRef = useRef<Map<string, AbortController>>(new Map());
// Forward-declared so ``handleRunnerEvent`` (created above
@@ -2551,7 +2557,8 @@ export function ChatStateAdapterProvider({
session.preferences?.selected_branches,
),
});
- if (loadedStatus === "running" && (activeTurn?.turn_id || activeTurn?.id)) {
+ const activeTurnId = activeTurn?.turn_id || activeTurn?.id || "";
+ if (activeTurnId && loadedStatus === "running") {
// Reached on a revalidate too, when the turn is live on the server but
// not in this tab (started in another tab, or our socket dropped) —
// that is exactly the case that still needs a subscribe. A turn we
@@ -2559,13 +2566,33 @@ export function ChatStateAdapterProvider({
// socket for a turn that will never speak again.
sendThroughRunner(key, {
type: "subscribe_turn",
- turn_id: activeTurn.turn_id || activeTurn.id,
+ turn_id: activeTurnId,
after_seq: 0,
});
+ } else if (activeTurnId && String(session.status || "") === "waiting_input") {
+ // A parked turn still owns the conversation's lease, and its ask_user
+ // card only lives in the live event stream: nothing about the pause is
+ // persisted as a message until the turn settles. Without a
+ // resubscribe, a reload loses the card while the turn keeps waiting —
+ // the user can neither answer nor send, and only a backend restart
+ // recovers the conversation. Send through the client's durable queue
+ // rather than ``sendThroughRunner``: its retry gate drops a command
+ // for a session that is not streaming, and a parked turn is exactly
+ // that. Once per turn per tab is enough; the reducer de-duplicates a
+ // replayed stream anyway.
+ const resumeKey = `${key}:${activeTurnId}`;
+ if (!resumedParkedTurnsRef.current.has(resumeKey)) {
+ resumedParkedTurnsRef.current.add(resumeKey);
+ ensureRunner(key).client.send({
+ type: "subscribe_turn",
+ turn_id: activeTurnId,
+ after_seq: 0,
+ });
+ }
}
return messages;
},
- [hydrateMessages, sendThroughRunner],
+ [ensureRunner, hydrateMessages, sendThroughRunner],
);
useLayoutEffect(() => {
diff --git a/web/tests/chat-failed-submission-recovery.spec.tsx b/web/tests/chat-failed-submission-recovery.spec.tsx
index 6aaf2581b..cb2c40525 100644
--- a/web/tests/chat-failed-submission-recovery.spec.tsx
+++ b/web/tests/chat-failed-submission-recovery.spec.tsx
@@ -1143,3 +1143,53 @@ it("keeps a stale tab's retry after another tab cleared the same submission ID",
vi.useRealTimers();
}
});
+
+it("resubscribes a parked ask_user turn on load so its card survives a reload", async () => {
+ fixture.session = {
+ ...completedServerSession(),
+ status: "waiting_input",
+ active_turns: [{ turn_id: "turn-parked", status: "waiting_input" }],
+ messages: [
+ {
+ id: 1,
+ session_id: "s1",
+ role: "user",
+ content: "Beach or mountains?",
+ events: [],
+ attachments: [],
+ created_at: 1,
+ parent_message_id: null,
+ },
+ ],
+ };
+
+ render(
+ <ChatStateAdapterProvider>
+ <Harness />
+ </ChatStateAdapterProvider>,
+ );
+ await act(async () => {
+ fireEvent.click(screen.getByText("Load"));
+ });
+
+ const subscribe = (message: Record<string, unknown>) =>
+ message.type === "subscribe_turn" &&
+ message.turn_id === "turn-parked" &&
+ message.after_seq === 0;
+ expect(fixture.sent.some(subscribe)).toBe(true);
+
+ // Re-opening the same parked turn must not subscribe twice; the replayed
+ // stream would otherwise be fetched again on every revalidate.
+ const subscribed = fixture.sent.filter(
+ (message) => message.type === "subscribe_turn",
+ ).length;
+ await act(async () => {
+ fireEvent.click(screen.getByText("Load"));
+ });
+ expect(screen.getByTestId("messages").textContent).toContain(
+ "Beach or mountains?",
+ );
+ expect(
+ fixture.sent.filter((message) => message.type === "subscribe_turn").length,
+ ).toBe(subscribed);
+});
diff --git a/web/tests/chat-idle-recovery.test.ts b/web/tests/chat-idle-recovery.test.ts
index 08502b5ff..519f31d4f 100644
--- a/web/tests/chat-idle-recovery.test.ts
+++ b/web/tests/chat-idle-recovery.test.ts
@@ -113,7 +113,14 @@ test("the loader vets the stored status and skips subscribing to a stale turn",
assert.match(adapter, /status: restoredStatus,/);
assert.match(
adapter,
- /if \(loadedStatus === "running" && \(activeTurn\?\.turn_id \|\| activeTurn\?\.id\)\)/,
+ /const activeTurnId = activeTurn\?\.turn_id \|\| activeTurn\?\.id \|\| "";/,
+ );
+ assert.match(adapter, /if \(activeTurnId && loadedStatus === "running"\)/);
+ // The parked (waiting_input) resubscribe is gated the same way: only an
+ // actively parked turn opens a socket for its stream.
+ assert.match(
+ adapter,
+ /else if \(activeTurnId && String\(session\.status \|\| ""\) === "waiting_input"\)/,
);
// And a row with nothing in it is not a message. One such row used to be