"""Session feedback (ajieval sessions), offline: transcript parsing, redaction, idempotent scans and the report thresholds.""" import io import json import os import sys import tempfile import unittest from unittest.mock import patch HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(HERE)) from ajieval import sessions # noqa: E402 CFG = {"sessions": {"min_correction": 0.7, "min_rule_confidence": 0.6, "min_events": 2, "min_sessions": 2}} def entry(kind, content, uuid, session="s1", **kw): return dict({"type": kind, "uuid": uuid, "sessionId": session, "message": {"role": kind, "content": content}}, **kw) def transcript(path, session="s1", prompt="no, you must not commit without asking"): rows = [ entry("user", "first request", "u0", session), entry("assistant", [{"type": "text", "text": "Done, committed."}, {"type": "tool_use", "name": "Bash", "input": {"command": "git commit -m x"}}], "a1", session), entry("user", [{"type": "tool_result", "content": "ok"}], "t1", session), entry("user", "/foo", "c1", session), entry("user", "meta", "m1", session, isMeta=True), entry("assistant", [{"type": "text", "text": "Key is sk-ABCDEFGHIJKLMNOPQRSTUV"}], "a2", session), entry("user", prompt, "u1", session), entry("assistant", [{"type": "text", "text": "side"}], "x1", session, isSidechain=True), ] with open(path, "w") as f: for r in rows: f.write(json.dumps(r) + "\n") def canned(correction=0.9, rule="commit_rules", confidence=0.8): calls = [] def post(body): calls.append(body) return {"model": "jev-test", "answers": {"correction": {"type": "noul", "noul": correction}, "rule": {"type": "choice", "choice": rule, "confidence": confidence}}} return post, calls class Sessions(unittest.TestCase): def test_episodes_skip_tool_results_meta_commands_and_sidechains(self): with tempfile.TemporaryDirectory() as d: p = os.path.join(d, "t.jsonl") transcript(p) eps = sessions.episodes(p) self.assertEqual([e[0] for e in eps], ["u1"]) uuid, session, assistant, prompt = eps[0] self.assertIn("[Bash] git commit -m x", assistant) self.assertIn("[redacted]", assistant) self.assertNotIn("sk-ABCDEFGHIJ", assistant) self.assertEqual((session, prompt), ("s1", "no, you must not commit without asking")) def test_scan_is_idempotent_and_sends_only_the_episode(self): with tempfile.TemporaryDirectory() as d: p, log = os.path.join(d, "t.jsonl"), os.path.join(d, "log", "events.jsonl") transcript(p) post, calls = canned() self.assertEqual(sessions.scan(p, post, log), 1) self.assertEqual(sessions.scan(p, post, log), 0) self.assertEqual(len(calls), 1) self.assertEqual(set(calls[0]["state"]), {"assistant_before", "user_prompt"}) self.assertIn("none", calls[0]["questions"]["rule"]["criteria"]) def test_report_needs_recurrence_across_sessions(self): with tempfile.TemporaryDirectory() as d: log = os.path.join(d, "events.jsonl") for i, s in enumerate(["s1", "s1"]): p = os.path.join(d, f"t{i}.jsonl") transcript(p, s, prompt=f"wrong {i}") with open(p) as f: rows = [json.loads(l) for l in f] with open(p, "w") as f: for r in rows: r["uuid"] = f"{r['uuid']}-{i}" f.write(json.dumps(r) + "\n") sessions.scan(p, canned()[0], log) rows = sessions.report(CFG, log) self.assertEqual((rows[0]["rule"], rows[0]["corrections"], rows[0]["proposed"]), ("commit_rules", 2, False)) p = os.path.join(d, "t9.jsonl") transcript(p, "s2") sessions.scan(p, canned()[0], log) rows = sessions.report(CFG, log) self.assertTrue(rows[0]["proposed"]) self.assertIn("memory: no-commit-attribution", sessions.render(rows)) def test_low_confidence_and_non_corrections_are_not_attributed(self): with tempfile.TemporaryDirectory() as d: log = os.path.join(d, "events.jsonl") p = os.path.join(d, "t.jsonl") transcript(p) sessions.scan(p, canned(correction=0.2)[0], log) self.assertEqual(sessions.report(CFG, log), []) os.remove(log) sessions.scan(p, canned(confidence=0.3)[0], log) self.assertEqual(sessions.report(CFG, log)[0]["rule"], "unattributed") def test_hook_without_key_does_nothing(self): with tempfile.TemporaryDirectory() as d: p = os.path.join(d, "t.jsonl") transcript(p) with patch("ajieval.jev.api_key", return_value=None), patch("subprocess.Popen") as popen, \ patch("sys.stdin", io.StringIO(json.dumps({"transcript_path": p}))): sessions.hook() popen.assert_not_called() if __name__ == "__main__": unittest.main()