diff --git a/hooks/langfuse_hook.py b/hooks/langfuse_hook.py index cdd8f64..919c319 100644 --- a/hooks/langfuse_hook.py +++ b/hooks/langfuse_hook.py @@ -2153,18 +2153,64 @@ def build_turn_history_messages(turn: Turn) -> List[Dict[str, Any]]: @dataclass class SessionHistory: - """Flat ChatML view of the whole transcript, with one index per turn.""" + """Flat ChatML view of the whole transcript, with one index per turn. + + A turn's prefix starts at its context segment: after a /compact (manual or + automatic) the model no longer sees the earlier messages, only the compact + summary and what the compaction preserved. + """ messages: List[Dict[str, Any]] = field(default_factory=list) start_index_by_user_row_uuid: Dict[str, int] = field(default_factory=dict) + segment_start_index_by_user_row_uuid: Dict[str, int] = field(default_factory=dict) def prefix_for_turn(self, user_row_uuid: Any) -> Optional[List[Dict[str, Any]]]: - """All messages before the given turn, or None when the turn is unknown.""" + """Messages of the turn's context segment before it, or None when the turn is unknown.""" if not isinstance(user_row_uuid, str) or not user_row_uuid: return None start_index = self.start_index_by_user_row_uuid.get(user_row_uuid) if start_index is None: return None - return self.messages[:start_index] + segment_start_index = self.segment_start_index_by_user_row_uuid.get(user_row_uuid, 0) + return self.messages[segment_start_index:start_index] + + +def is_compact_boundary_row(row: Dict[str, Any]) -> bool: + return row.get("type") == "system" and row.get("subtype") == "compact_boundary" + + +def get_compact_preserved_rows(boundary_row: Dict[str, Any], rows: List[Dict[str, Any]], + index_by_uuid: Dict[str, int]) -> List[Dict[str, Any]]: + """Chat rows a compaction kept verbatim (compactMetadata.preservedSegment head..tail).""" + metadata = boundary_row.get("compactMetadata") + segment = metadata.get("preservedSegment") if isinstance(metadata, dict) else None + if not isinstance(segment, dict): + return [] + head_index = index_by_uuid.get(segment.get("headUuid")) + tail_index = index_by_uuid.get(segment.get("tailUuid")) + if head_index is None or tail_index is None or tail_index < head_index: + return [] + return [ + row for row in rows[head_index:tail_index + 1] + if row.get("type") in ("user", "assistant") and not row.get("isMeta") + ] + + +def build_loose_row_history_messages(rows: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + """ChatML for rows outside any assembled turn (a compaction's preserved segment).""" + messages: List[Dict[str, Any]] = [] + for row in rows: + content = get_content_from_row(row) + if get_user_or_assistant_role_from_row(row) == "assistant": + assistant_text, _ = truncate_text(extract_text_from_content(content)) + messages.append(build_generation_output(assistant_text, get_tool_use_blocks(content))) + elif is_tool_result(row): + messages.append({"role": "tool", "tool_results": [ + {"tool_use_id": block.get("tool_use_id"), "output": truncate_text(get_tool_result_text(block))[0]} + for block in get_tool_result_blocks(content) + ]}) + else: + messages.append({"role": "user", "content": build_user_history_content(row)}) + return messages def build_session_history( @@ -2174,17 +2220,47 @@ def build_session_history( """Rebuild the conversation of the whole transcript file as ChatML. Reads from byte 0 on purpose: the emission offset only tracks what was - exported, while a generation input needs every earlier message. + exported, while a generation input needs every earlier message of its + context window. Each compact boundary starts a new window. """ rows = read_subagent_jsonl(transcript_path) if not rows: return None + index_by_uuid = {row.get("uuid"): i for i, row in enumerate(rows) if isinstance(row.get("uuid"), str)} + # Split at each compact boundary: a segment is one context window. + segments: List[List[Dict[str, Any]]] = [[]] + preserved_rows_by_segment: List[List[Dict[str, Any]]] = [[]] + for row in rows: + if is_compact_boundary_row(row): + segments.append([]) + preserved_rows_by_segment.append(get_compact_preserved_rows(row, rows, index_by_uuid)) + else: + segments[-1].append(row) + history = SessionHistory() - for turn in build_turns(rows, task_id_to_tool_use_id): - user_row_uuid = turn.user_msg.get("uuid") - if isinstance(user_row_uuid, str) and user_row_uuid: - history.start_index_by_user_row_uuid.setdefault(user_row_uuid, len(history.messages)) - history.messages.extend(build_turn_history_messages(turn)) + for segment_rows, preserved_rows in zip(segments, preserved_rows_by_segment): + segment_start_index = len(history.messages) + turns = build_turns(segment_rows, task_id_to_tool_use_id) + summary_row = next((row for row in segment_rows if row.get("isCompactSummary")), None) + # The window after a compaction reads: summary, preserved messages, then + # the turns that follow. When work resumed right after an automatic + # compaction, the summary row itself opens the first turn. + preserved_messages = build_loose_row_history_messages(preserved_rows) + if summary_row is not None and not any(turn.user_msg is summary_row for turn in turns): + history.messages.append({"role": "user", "content": build_user_history_content(summary_row)}) + history.messages.extend(preserved_messages) + preserved_messages = [] + for turn in turns: + user_row_uuid = turn.user_msg.get("uuid") + if isinstance(user_row_uuid, str) and user_row_uuid: + history.start_index_by_user_row_uuid.setdefault(user_row_uuid, len(history.messages)) + history.segment_start_index_by_user_row_uuid.setdefault(user_row_uuid, segment_start_index) + turn_messages = build_turn_history_messages(turn) + if turn.user_msg is summary_row and preserved_messages: + turn_messages = turn_messages[:1] + preserved_messages + turn_messages[1:] + preserved_messages = [] + history.messages.extend(turn_messages) + history.messages.extend(preserved_messages) return history diff --git a/tests/integration/test_compacted_session_history.py b/tests/integration/test_compacted_session_history.py new file mode 100644 index 0000000..e908a69 --- /dev/null +++ b/tests/integration/test_compacted_session_history.py @@ -0,0 +1,149 @@ +"""Generation inputs after a /compact show the context window the model had. + +After a compaction (manual /compact or automatic), Claude Code continues from +the compact summary plus any preserved messages. Messages before the compact +boundary are no longer in the context window, so they must not appear in the +input of later generations. +""" +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +SESSION = "dddddddd-4444-4444-4444-444444444444" +SUMMARY_TEXT = "This session is being continued from a previous conversation. Summary: ..." + + +def user_row(index: int, text: str) -> dict[str, Any]: + return { + "type": "user", + "timestamp": f"2026-08-11T14:{index:02d}:00.000Z", + "sessionId": SESSION, + "uuid": f"user-row-{index}", + "message": {"role": "user", "content": text}, + } + + +def assistant_row(index: int, text: str) -> dict[str, Any]: + return { + "type": "assistant", + "timestamp": f"2026-08-11T14:{index:02d}:10.000Z", + "sessionId": SESSION, + "uuid": f"assistant-row-{index}", + "message": { + "id": f"msg-{index}", + "role": "assistant", + "model": "claude-test", + "content": [{"type": "text", "text": text}], + "usage": {"input_tokens": 2, "output_tokens": 5}, + }, + } + + +def compact_boundary_row(index: int, preserved: tuple[str, str] | None = None) -> dict[str, Any]: + metadata: dict[str, Any] = {"trigger": "manual", "preTokens": 1000} + if preserved: + head, tail = preserved + metadata["preservedSegment"] = {"headUuid": head, "anchorUuid": tail, "tailUuid": tail} + return { + "type": "system", + "subtype": "compact_boundary", + "content": "Conversation compacted", + "timestamp": f"2026-08-11T14:{index:02d}:00.000Z", + "sessionId": SESSION, + "uuid": f"boundary-{index}", + "isMeta": False, + "compactMetadata": metadata, + } + + +def summary_row(index: int) -> dict[str, Any]: + return { + "type": "user", + "timestamp": f"2026-08-11T14:{index:02d}:01.000Z", + "sessionId": SESSION, + "uuid": f"summary-{index}", + "isCompactSummary": True, + "isVisibleInTranscriptOnly": True, + "message": {"role": "user", "content": SUMMARY_TEXT}, + } + + +def turn(index: int) -> list[dict[str, Any]]: + return [user_row(index, f"Question {index}?"), assistant_row(index, f"Answer {index}.")] + + +def write_rows(path: Path, rows: list[dict[str, Any]]) -> None: + path.write_text("\n".join(json.dumps(row) for row in rows) + "\n", encoding="utf-8") + + +def emit(hook_module: Any, fake_langfuse: Any, transcript: Path) -> list[Any]: + config = hook_module.LangfuseConfig("public", "secret", "https://example.test", "user-1") + hook_module.emit_new_turns_from_transcript(fake_langfuse, config, SESSION, transcript) + return [o.kwargs.get("input") for o in fake_langfuse.observations if o.as_type == "generation"] + + +def contents(messages: list[dict[str, Any]]) -> list[Any]: + return [m.get("content") for m in messages] + + +def test_turn_after_compact_sees_summary_not_earlier_turns( + hook_module, fake_langfuse, isolated_hook_state, tmp_path +): + transcript = tmp_path / f"{SESSION}.jsonl" + write_rows(transcript, turn(1) + turn(2) + [compact_boundary_row(3), summary_row(3)] + turn(4)) + + inputs = emit(hook_module, fake_langfuse, transcript) + + assert len(inputs) == 3 + # Before the compaction the history is unchanged. + assert contents(inputs[1]) == ["Question 1?", "Answer 1.", "Question 2?"] + # After it, the window is the summary and the new turn only. + assert contents(inputs[2]) == [SUMMARY_TEXT, "Question 4?"] + + +def test_preserved_segment_follows_the_summary( + hook_module, fake_langfuse, isolated_hook_state, tmp_path +): + transcript = tmp_path / f"{SESSION}.jsonl" + rows = ( + turn(1) + turn(2) + + [compact_boundary_row(3, preserved=("user-row-2", "assistant-row-2")), summary_row(3)] + + turn(4) + ) + write_rows(transcript, rows) + + inputs = emit(hook_module, fake_langfuse, transcript) + + assert contents(inputs[-1]) == [SUMMARY_TEXT, "Question 2?", "Answer 2.", "Question 4?"] + + +def test_work_resumed_after_auto_compact_keeps_the_summary_turn( + hook_module, fake_langfuse, isolated_hook_state, tmp_path +): + # An automatic compaction in the middle of work: the summary row is the user + # row in front of the resumed assistant reply, so it opens a turn itself. + transcript = tmp_path / f"{SESSION}.jsonl" + rows = turn(1) + [compact_boundary_row(2), summary_row(2), assistant_row(3, "Resumed.")] + turn(4) + write_rows(transcript, rows) + + inputs = emit(hook_module, fake_langfuse, transcript) + + assert contents(inputs[-1]) == [SUMMARY_TEXT, "Resumed.", "Question 4?"] + + +def test_each_compaction_starts_a_new_window( + hook_module, fake_langfuse, isolated_hook_state, tmp_path +): + transcript = tmp_path / f"{SESSION}.jsonl" + rows = ( + turn(1) + [compact_boundary_row(2), summary_row(2)] + turn(3) + + [compact_boundary_row(4), summary_row(4)] + turn(5) + ) + write_rows(transcript, rows) + + inputs = emit(hook_module, fake_langfuse, transcript) + + assert contents(inputs[1]) == [SUMMARY_TEXT, "Question 3?"] + assert contents(inputs[2]) == [SUMMARY_TEXT, "Question 5?"]