From 22df77a9d6f1b4dc39d95e6e96d5eac46c634fb0 Mon Sep 17 00:00:00 2001 From: Saksham Uboweja <76595406+SakshamUboweja@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:34:42 +0000 Subject: [PATCH] plugin: mark a folded session on its current document, not the fold's first read fold_locked read sessions/.json when it started and wrote that copy back with folded=true when it ended, seconds later when the proposer ran. A session resumed with the same id in between (claude -p --resume right after the first run) lost what its hooks wrote meanwhile: the loop guard's open turn and its calls, and source=resume. The flag is now set on the document as it is at the end of the fold. --- tests/test_live_hooks.py | 28 ++++++++++++++++++++++++++++ tokeneconomizer/plugin/fold.py | 9 ++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/test_live_hooks.py b/tests/test_live_hooks.py index 0513edaa..e1e56705 100644 --- a/tests/test_live_hooks.py +++ b/tests/test_live_hooks.py @@ -953,6 +953,34 @@ def test_a_fold_that_cannot_take_the_lock_skips_and_the_session_is_still_folded_ assert len(st.read_lines("ledger.jsonl")) == 1 +def test_a_session_resumed_while_its_fold_runs_keeps_what_its_hooks_wrote(env, repo, tmp_path): + """The fold reads the session's document when it starts and marks it folded when it ends, + seconds later when the proposer runs. A `--resume` of the same session in between writes + its own turn and source there; marking the document folded must not put the old copy back.""" + t = trap_session(str(tmp_path / "s.jsonl"), repo, 0) + base = {"session_id": "s", "transcript_path": t, "cwd": repo} + call(env, dict(base, hook_event_name="SessionStart", source="startup")) + call(env, dict(base, hook_event_name="UserPromptSubmit", prompt_id="p1", prompt="hi")) + call(env, dict(base, hook_event_name="Stop", prompt_id="p1")) + ctx = LiveContext.build(hosts.HOSTS["claude"](), repo, env) + usage = ctx.host.session_usage + + def resumed_meanwhile(transcript): # the fold's slow part, with the resume's hooks in it + call(env, dict(base, hook_event_name="SessionStart", source="resume")) + call(env, dict(base, hook_event_name="UserPromptSubmit", prompt_id="p2", prompt="again")) + for i in range(3): + call(env, dict(base, hook_event_name="PostToolUse", prompt_id="p2", tool_name="Bash", + tool_input={"command": f"ls {i}"}, + tool_response={"stdout": "x", "stderr": "", "interrupted": False})) + return usage(transcript) + ctx.host.session_usage = resumed_meanwhile + assert fold.fold_session(ctx, "s") is not None + sess = store_for(env, repo).read(os.path.join("sessions", "s.json")) + assert sess["folded"] is True + assert sess["source"] == "resume" + assert (sess["turn"]["turn_key"], sess["turn"]["cum_turns"]) == ("p2", 3) + + def test_a_session_whose_end_was_missed_is_folded_by_the_next_start(env, repo, tmp_path): t = trap_session(str(tmp_path / "lost.jsonl"), repo, 0) call(env, {"hook_event_name": "SessionStart", "session_id": "lost", "source": "startup", diff --git a/tokeneconomizer/plugin/fold.py b/tokeneconomizer/plugin/fold.py index a99e0d58..ea817614 100644 --- a/tokeneconomizer/plugin/fold.py +++ b/tokeneconomizer/plugin/fold.py @@ -538,9 +538,12 @@ def fold_locked(ctx, session_id, transcript_path=None, imported=False, span=None entry["imported"] = True folded[session_id] = entry ctx.store.write("folded.json", folded) - if sess: - sess["folded"] = True - ctx.store.write(sess_name, sess) + # Re-read: the copy read above may be seconds old by now, and the same session, resumed + # while this fold ran, may have hooks that wrote to it since (its turn, its source). + cur = ctx.store.read(sess_name) if sess else None + if isinstance(cur, dict): + cur["folded"] = True + ctx.store.write(sess_name, cur) return record finally: shutil.rmtree(workdir, ignore_errors=True)