Event log: return the per-session seq from append_event, not the row id
append_event handed back cur.lastrowid — the table-wide row id — while the INSERT allocates a per-session seq and read_events returns that column. The two only agree while a single session exists, so ask_user's question_seq never matched latest_open_question() on a real install: a card click never wrote question/answered and never counted as a structured approval. Read the allocated seq back for the inserted row and return it. Two regression tests seed a second session first so row id != seq. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
f4424a5c4f
commit
693656757c
@@ -112,7 +112,14 @@ def append_event(session_id: int, event_type: str, data: dict[str, Any]) -> Sess
|
||||
""",
|
||||
(session_id, session_id, event_type, json.dumps(data, ensure_ascii=False, default=str)),
|
||||
)
|
||||
seq = cur.lastrowid
|
||||
# cur.lastrowid is the table-wide row id, NOT the per-session seq the
|
||||
# INSERT just allocated; read the seq back. Returning the row id made
|
||||
# ask_user hand the card a question_seq that latest_open_question()
|
||||
# (which compares per-session seq) could never match, so a card click
|
||||
# never wrote question/answered once more than one session existed.
|
||||
seq = conn.execute(
|
||||
"SELECT seq FROM agent_events WHERE id = ?", (cur.lastrowid,)
|
||||
).fetchone()["seq"]
|
||||
conn.execute(
|
||||
"UPDATE agent_sessions SET updated_at = CURRENT_TIMESTAMP WHERE id = ?",
|
||||
(session_id,),
|
||||
|
||||
@@ -250,3 +250,54 @@ def test_ask_user_logs_tool_result_before_turn_end(client, session):
|
||||
assert tr.data["result"]["question"] == "Regenerate the script?"
|
||||
end = events[end_idx]
|
||||
assert end.data["status"] == "awaiting_input", end.data
|
||||
|
||||
|
||||
def test_append_event_returns_per_session_seq(client, session):
|
||||
"""append_event must return the per-session seq it allocated, not the
|
||||
table-wide row id. Once any other session has events the two diverge, and
|
||||
ask_user's question_seq (taken from the returned event) must still match
|
||||
what read_events / latest_open_question report for the same row."""
|
||||
other = client.post("/api/agent/sessions", json={}).json()
|
||||
for i in range(3):
|
||||
session_log.append_event(other["id"], "assistant/message", {"content": f"noise {i}"})
|
||||
|
||||
ev = session_log.append_event(
|
||||
session["id"],
|
||||
session_log.QUESTION_ASKED,
|
||||
{"question": "q?", "options": ["a", "b"], "scope": "info"},
|
||||
)
|
||||
events = session_log.read_events(session["id"])
|
||||
stored = [e for e in events if e.type == session_log.QUESTION_ASKED]
|
||||
assert len(stored) == 1
|
||||
assert ev.seq == stored[0].seq
|
||||
assert interaction.latest_open_question(session["id"])["seq"] == ev.seq
|
||||
|
||||
|
||||
def test_card_answer_matches_question_when_other_sessions_exist(client, session):
|
||||
"""The card click flow end to end with a second, busier session in the
|
||||
table: question_seq handed to the card must still record question/answered."""
|
||||
import asyncio
|
||||
|
||||
other = client.post("/api/agent/sessions", json={}).json()
|
||||
for i in range(5):
|
||||
session_log.append_event(other["id"], "assistant/message", {"content": f"noise {i}"})
|
||||
|
||||
ctx = ToolContext(session_id=session["id"], project_id=None)
|
||||
asked = asyncio.run(
|
||||
execute_tool(
|
||||
ctx,
|
||||
"ask_user",
|
||||
{"question": "Render now?", "options": ["Yes", "No"], "scope": "render"},
|
||||
)
|
||||
)
|
||||
resp = client.post(
|
||||
f"/api/agent/sessions/{session['id']}/messages",
|
||||
json={"content": "Yes", "answer_to": asked["question_seq"]},
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
events = session_log.read_events(session["id"])
|
||||
answered = [e for e in events if e.type == session_log.QUESTION_ANSWERED]
|
||||
assert len(answered) == 1
|
||||
assert answered[0].data["question_seq"] == asked["question_seq"]
|
||||
assert policy.has_structured_approval(ctx, "render") is True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user