Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(studio): price the transcript link in the whole-document retry
  • Loading branch information
Lyxot committed Nov 27, 2026
commit 663dafe020fc1936eab58c4c6c2589dfb882e740
15 changes: 14 additions & 1 deletion studio/backend/core/inference/llama_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,17 @@ def _has_unpriced_media(messages: list[dict]) -> bool:
)


def _with_transcript_link(messages: list[dict], thread_id: Optional[str]) -> list[dict]:
"""The prompt as `_archive_and_recall` will send it, with the transcript link it adds."""
from core.inference import compaction_transcript
from core.rag import conversation_archive

if not thread_id or not conversation_archive.enabled():
return messages
path = compaction_transcript.path_for(thread_id)
return compaction_transcript.link_in_block(messages, path) if path else messages


def _records_boundary(truncation: dict) -> bool:
"""Not the fits flag: a rescued fit still evicts, and skipping the save compacts silently. Saving is not replaying: sticky logic declines fits-false records."""
return bool(truncation.get("fits")) or bool(truncation.get("dropped_messages"))
Expand Down Expand Up @@ -42294,7 +42305,9 @@ def _count() -> None:
)
candidate = base[:at] + refreshed["messages"] + base[at:]
measured = _fit_count_tokens(
candidate, safe_tools, _reasoning_kw
_with_transcript_link(candidate, thread_id),
safe_tools,
_reasoning_kw,
)
# The fit's own target, also on a replay: headroom, plus the recall reserve a rolling fit holds back.
budget = prompt_budget(
Expand Down
22 changes: 20 additions & 2 deletions studio/backend/tests/test_llama_cpp_tool_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7821,9 +7821,25 @@ def test_first_context_fit_retries_whole_document_in_prompt_order(
llama_cpp, "_keeps_compaction_boundary", lambda thread_id: thread_id == "t1"
)
recall_budgets = []
from core.inference import compaction_transcript
from core.rag import conversation_archive

monkeypatch.setattr(conversation_archive, "enabled", lambda: True)
monkeypatch.setattr(compaction_transcript, "path_for", lambda _thread_id: "/chat/transcript.md")
link = " Transcript at /chat/transcript.md."

def link_in_block(messages, _path):
system = messages[0]
if system["role"] != "system" or system["content"].endswith(link):
return messages
return [{**system, "content": system["content"] + link}, *messages[1:]]

monkeypatch.setattr(compaction_transcript, "link_in_block", link_in_block)

def archive_and_recall(conversation, *_a, recall_budget_tokens, **_kw):
recall_budgets.append(recall_budget_tokens)
if _kw["thread_id"]:
conversation = compaction_transcript.link_in_block(conversation, "/chat/transcript.md")
return dict(conversation = conversation, counts = {}, recalled = False)

monkeypatch.setattr(llama_cpp, "_archive_and_recall", archive_and_recall)
Expand Down Expand Up @@ -7883,6 +7899,7 @@ def inject(
events = list(
backend.generate_chat_completion_with_tools(
messages = [
{"role": "system", "content": "Be brief."},
{"role": "user", "content": "ancient " * (5 if manual else 250)},
{"role": "assistant", "content": "Prior answer"},
{
Expand All @@ -7895,7 +7912,7 @@ def inject(
context_overflow = "truncate_oldest",
context_policy = policy,
compaction_headroom_ratio = 0.25,
thread_id = "t1" if policy == "rolling" else None,
thread_id = "t1" if policy == "rolling" else "t2",
max_tokens = 80,
max_tool_iterations = 2,
tool_choice = tool_choice,
Expand All @@ -7913,9 +7930,10 @@ def inject(
truncation = next(e for e in events if e["type"] == "context_truncated")
assert truncation["fits"] and truncation["dropped_messages"] == 2
if not audio:
# An accepted retry prices the transcript link too; a plain fit leaves it uncounted, as before.
assert truncation["prompt_tokens_after"] == sum(
len(str(m.get("content", ""))) for m in payloads[0]["messages"]
)
) - len(link) * (expected_passage != complete)
from core.inference.context_window import prompt_budget, retrieval_budget

prompt = truncation["prompt_tokens_after"]
Expand Down