From 14d9bf1bcdc6f13bbc14e079a05c1cdadf661ea6 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Mon, 30 Nov 2026 20:31:26 +0100 Subject: [PATCH] cli: rate the episode of the exchange being rated /good, /bad and /fix recorded on the latest episode of the last session run. After /undo, or after a direct command whose answer has no episode, that is not the answer at the end of the history: the verdict or the correction went to an exchange the operator had just removed, or to the one before a !read. Each answer now carries the session that produced it (None for a direct command), and the verdicts look up that session's episode. A history saved before falls back to the last session, as before. --- client/cli/rag_chat.py | 30 ++++++++++++++++++++------ client/tests/test_operator_verdicts.py | 27 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/client/cli/rag_chat.py b/client/cli/rag_chat.py index f5ff238..9058d04 100755 --- a/client/cli/rag_chat.py +++ b/client/cli/rag_chat.py @@ -301,6 +301,17 @@ def session_command(argument, history, lock): return history +def exchange_episode(store, history, last_session_id): + """The training episode of the exchange at the end of the history. + + It is the one its answer was recorded with, not the last one run: /undo + and a direct command both leave the last session pointing at an answer + that is no longer the one being rated.""" + + session_id = history[-1].get("session_id", last_session_id) + return store.latest_episode_id(session_id) if session_id else None + + def main(): global TRACE_PROVIDER, TRACE_MODEL @@ -521,7 +532,8 @@ def read_user_input(): else 2000) entry = f"[tool result]\n{result[:kept]}" history.append({"role": "user", "content": user_input}) - history.append({"role": "assistant", "content": entry}) + history.append({"role": "assistant", "content": entry, + "session_id": None}) archive_entry({"role": "user", "content": user_input}) archive_entry({"role": "assistant", "content": entry}) save_history(history) @@ -811,9 +823,10 @@ def read_user_input(): recorded = False - if TRAINING_STORE is not None and last_session_id: + if TRAINING_STORE is not None: try: - episode_id = TRAINING_STORE.latest_episode_id(last_session_id) + episode_id = exchange_episode(TRAINING_STORE, history, + last_session_id) if episode_id: TRAINING_STORE.record_verdict(episode_id, user_input[1:]) @@ -862,9 +875,10 @@ def read_user_input(): episode_id = None - if TRAINING_STORE is not None and last_session_id: + if TRAINING_STORE is not None: try: - episode_id = TRAINING_STORE.latest_episode_id(last_session_id) + episode_id = exchange_episode(TRAINING_STORE, history, + last_session_id) if episode_id: TRAINING_STORE.record_correction(episode_id, text) @@ -1649,7 +1663,8 @@ def keep_cut_turn(response, reason): if reply: history.append({"role": "user", "content": user_input}) - history.append({"role": "assistant", "content": reply}) + history.append({"role": "assistant", "content": reply, + "session_id": last_session_id}) save_history(history) if task_result.status == TaskStatus.INTERRUPTED: @@ -1811,7 +1826,8 @@ def keep_cut_turn(response, reason): archive_entry({"role": "user", "content": user_record}) if full_response: - history.append({"role": "assistant", "content": full_response}) + history.append({"role": "assistant", "content": full_response, + "session_id": last_session_id}) archive_entry({"role": "assistant", "content": full_response}) if len(history) > MAX_HISTORY: diff --git a/client/tests/test_operator_verdicts.py b/client/tests/test_operator_verdicts.py index 078a264..0cb9817 100644 --- a/client/tests/test_operator_verdicts.py +++ b/client/tests/test_operator_verdicts.py @@ -5,6 +5,7 @@ and stayed a positive example. """ +import dataclasses import tempfile import unittest from pathlib import Path @@ -47,6 +48,32 @@ def test_the_latest_episode_is_found(self): self.assertEqual(self.store.latest_episode_id(), "episode_b") self.assertIsNone(self.store.latest_episode_id("another-session")) + def test_a_verdict_goes_to_the_episode_of_the_exchange_it_rates(self): + from cli.rag_chat import exchange_episode + + for name in ("kept", "undone"): + self.store.save_episode(dataclasses.replace( + answered(f"episode_{name}"), session_id=f"session_{name}")) + + # /undo dropped the last exchange: the history now ends on the one + # before, while the last session run is still the undone one. + + history = [{"role": "user", "content": "q"}, + {"role": "assistant", "content": "a", "session_id": "session_kept"}] + + self.assertEqual(exchange_episode(self.store, history, "session_undone"), + "episode_kept") + + # A direct command's answer has no episode, and a history saved before + # answers carried their session falls back to the last one. + + history[-1]["session_id"] = None + self.assertIsNone(exchange_episode(self.store, history, "session_undone")) + + del history[-1]["session_id"] + self.assertEqual(exchange_episode(self.store, history, "session_undone"), + "episode_undone") + def test_a_bad_answer_is_never_an_sft_target(self): self.store.save_episode(answered("episode_a")) self.assertEqual(len(SFTDatasetBuilder(self.store).build().samples), 1)