session_rules/turns.py and activity.py carried comments copied from main that retell history rather than behaviour: issue and PR numbers, "Pre-#NNNN" tags on the historical summary prefixes, a reviewer's name and review number, and a dated incident note on SUMMARY_PREFIX. Each now says what the code does, or the reference is dropped where the sentence already says it; the historical prefixes keep their byte-pinned text and each keeps a line naming what that wording lacks. Comments and docstrings only: the parse tree with docstrings blanked is identical before and after. Reported-by: kshitijk4poor
1642 lines
86 KiB
Python
1642 lines
86 KiB
Python
"""The pure turn policy the store applies inside its transactions, shared by the application and every backend.
|
|
|
|
Which user rows are human turns (compaction carriers split into their handoff scaffold and live ask, synthetic nudges and display-only rows excluded), how a carrier projects for display, what /retry may resend, how content is sanitized and projected for the durable row, the alternation repair of a replayed transcript, tool-call identity across rows, and the skill-scaffold describer previews use. Standard library and the persistence contracts only.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import json
|
|
import logging
|
|
import re
|
|
import threading
|
|
from collections import Counter, OrderedDict
|
|
from collections.abc import Mapping, MutableMapping
|
|
from typing import Any, Optional
|
|
from uuid import uuid4
|
|
|
|
from persistence.conversation.messages import CONTENT_JSON_PREFIX, CONTENT_REFUSED_DISPLAY_KIND
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
_FENCE_TAG_RE = re.compile(r'</?\s*memory-context\s*>', re.IGNORECASE)
|
|
_INTERNAL_CONTEXT_RE = re.compile(r'<\s*memory-context\s*>[\s\S]*?</\s*memory-context\s*>', re.IGNORECASE)
|
|
_INTERNAL_NOTE_RE = re.compile(
|
|
r'\[System note:\s*The following is recalled memory context,\s*NOT new user input\.\s*Treat as (?:informational background data|authoritative reference data[^\]]*)\.\]\s*',
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def sanitize_context(text: str) -> str:
|
|
"""Strip fence tags, injected context blocks, and system notes from provider output."""
|
|
for pattern in (_INTERNAL_CONTEXT_RE, _INTERNAL_NOTE_RE, _FENCE_TAG_RE):
|
|
text = pattern.sub('', text)
|
|
return text
|
|
|
|
|
|
_NON_TEXT_PART_TYPES = {"image", "image_url", "input_image", "audio", "input_audio"}
|
|
_TEXT_KEYS = ("text", "content", "input_text", "output_text", "summary_text")
|
|
|
|
|
|
def _field(value: Any, key: str) -> Any:
|
|
return value.get(key) if isinstance(value, Mapping) else getattr(value, key, None)
|
|
|
|
|
|
def _text_from_part(part: Any) -> str:
|
|
if part is None:
|
|
return ""
|
|
if isinstance(part, str):
|
|
return part
|
|
if str(_field(part, "type") or "").strip().lower() in _NON_TEXT_PART_TYPES:
|
|
return ""
|
|
for key in _TEXT_KEYS:
|
|
text = _field(part, key)
|
|
if isinstance(text, str):
|
|
return text
|
|
return ""
|
|
|
|
|
|
def flatten_message_text(content: Any, *, sep: str = "\n") -> str:
|
|
"""Return the visible text from common chat/Responses message content shapes."""
|
|
if content is None:
|
|
return ""
|
|
if isinstance(content, str):
|
|
return content
|
|
if isinstance(content, list):
|
|
return sep.join(chunk for chunk in map(_text_from_part, content) if chunk)
|
|
text = _text_from_part(content)
|
|
if text:
|
|
return text
|
|
try:
|
|
return str(content)
|
|
except Exception: # health: allow BLE001 -- content whose str() raises renders as no text
|
|
return ""
|
|
|
|
|
|
def is_content_blank(content: Any) -> bool:
|
|
"""True when decoded message content is None, whitespace-only, or has no visible text parts."""
|
|
if content is None:
|
|
return True
|
|
if isinstance(content, str):
|
|
return not content.strip()
|
|
if isinstance(content, list):
|
|
return not "".join(p.get("text", "") for p in content if isinstance(p, dict) and p.get("type") == "text").strip()
|
|
return False
|
|
|
|
|
|
def drop_stale_api_content(msg: dict[str, Any]) -> None:
|
|
"""Drop the ``api_content`` sidecar from a message whose content was rewritten
|
|
(replaying it would resend what the rewrite removed; cost is one cache miss)."""
|
|
msg.pop("api_content", None)
|
|
|
|
|
|
STEER_DISPLAY_KIND = "steer"
|
|
|
|
|
|
PARTIAL_FAILED_TURN_NOTICE = (
|
|
"This turn did not complete. Some actions may already have run; verify their effects "
|
|
"before resending."
|
|
)
|
|
|
|
|
|
# ``messages.display_kind`` of that row: display-only (stripped before every provider request),
|
|
# so renderers show a Hermes notice and room pollers never read it as the model's reply.
|
|
FAILED_TURN_DISPLAY_KIND = "failed_turn"
|
|
|
|
|
|
TODO_INJECTION_HEADER = "[Your active task list was preserved across context compression]"
|
|
|
|
|
|
# Named so _is_synthetic_compression_user_turn can recognize a crash-persisted nudge by
|
|
# content (the store's projection strips the _length_continuation_nudge tag).
|
|
_LENGTH_CONTINUATION_NETWORK_STUB = (
|
|
"[System: The previous response was cut off by a network error mid-stream — a transport "
|
|
"interruption, NOT a change in your capabilities. Your tools are still fully available; call "
|
|
"them as normal and ignore any earlier claim that you lack tool access. Continue the task "
|
|
"from where you left off. Do not restart or repeat prior text.]"
|
|
)
|
|
_LENGTH_CONTINUATION_OUTPUT_LIMIT = (
|
|
"[System: Your previous response was truncated by the output length limit. Continue exactly "
|
|
"where you left off. Do not restart or repeat prior text. Finish the answer directly.]"
|
|
)
|
|
# An older wording of the network-error stub; kept so nudges persisted by older sessions are still recognized.
|
|
_LEGACY_LENGTH_CONTINUATION_NETWORK_STUB = (
|
|
"[System: The previous response was cut off by a network error mid-stream. Continue exactly "
|
|
"where you left off. Do not restart or repeat prior text. Finish the answer directly.]"
|
|
)
|
|
# The dropped-tools variant interpolates tool names; matched by prefix.
|
|
_LENGTH_CONTINUATION_DROPPED_TOOLS_PREFIX = "[System: Your previous tool call "
|
|
|
|
|
|
# Codex/Responses turns that returned only internal reasoning: a bare retry would be
|
|
# byte-identical, so the model repeats it.
|
|
_CODEX_INCOMPLETE_NUDGE = (
|
|
"[System: Your previous response contained only internal reasoning and never produced a "
|
|
"visible answer or tool call. Do not keep thinking. Produce your final answer as plain text "
|
|
"now (or make the tool call you were planning).]"
|
|
)
|
|
|
|
|
|
# Re-prompt after an acknowledgment-only Codex/Responses reply.
|
|
_CODEX_ACK_CONTINUATION_NUDGE = (
|
|
"[System: Continue now. Execute the required tool calls and only send your final answer "
|
|
"after completing the task.]"
|
|
)
|
|
|
|
# Re-prompt after a collapsed fragment ended a turn that had done real tool work. Asks
|
|
# for the same answer again when it WAS complete, so a false positive costs one call, never the answer.
|
|
_DEGENERATE_FINAL_NUDGE = (
|
|
"[System: Your previous message ended the turn with a fragment that is not a usable answer. "
|
|
"If the task is unfinished, continue it and then give the complete answer. If that fragment "
|
|
"WAS your complete answer, send it again exactly as before.]"
|
|
)
|
|
|
|
# Re-prompt for finish_reason="tool_calls" with empty tool_calls (an interrupt mid-retry can persist it).
|
|
_DROPPED_TOOLCALL_NUDGE_CONTENT = (
|
|
"Your previous turn indicated a tool call but none was included. Do not narrate a plan or "
|
|
"restate intent — issue the actual tool call now to continue the task."
|
|
)
|
|
|
|
# Re-prompt for an empty response after tool calls; the metadata flag does not
|
|
# survive the store's projection, so it is matched by content.
|
|
_EMPTY_TOOL_RESPONSE_NUDGE = (
|
|
"You just executed tool calls but returned an empty response. Please process the tool "
|
|
"results above and continue with the task."
|
|
)
|
|
|
|
|
|
HISTORICAL_TASK_HEADING = "## Historical Task Snapshot"
|
|
|
|
|
|
SUMMARY_PREFIX = (
|
|
# The "tools remain fully active" clause keeps the REFERENCE ONLY framing from suppressing tool use after a
|
|
# compression, and the latest message, not topic overlap, decides whether the summary's task resumes.
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted "
|
|
"into the summary below. This is a handoff from a previous context "
|
|
"window — treat it as background reference, NOT as active instructions. "
|
|
"Do NOT answer questions or fulfill requests mentioned in this summary; "
|
|
"they were already addressed. "
|
|
"Respond ONLY to the latest user message that appears AFTER this "
|
|
"summary — that message is the single source of truth for what to do "
|
|
"right now. "
|
|
"If no user message appears AFTER this summary, do nothing: do not "
|
|
"resume, wrap up, or continue work from "
|
|
f"'{HISTORICAL_TASK_HEADING}' or any other section, do not call tools, "
|
|
"and wait for a new user message. This handoff must never become the "
|
|
"active turn by itself. (Exception: if tool results or your own "
|
|
"tool calls appear after this summary, you are mid-way through an "
|
|
"in-flight exchange — continue that exchange normally.) "
|
|
"Topic overlap with the summary does NOT mean you should resume its "
|
|
"task: even on similar topics, the latest user message WINS. Treat ONLY "
|
|
"the latest message as the active task and discard stale items from "
|
|
f"'{HISTORICAL_TASK_HEADING}' entirely — do not 'wrap up' or "
|
|
"'finish' work described there unless the latest message explicitly "
|
|
"asks for it. "
|
|
"Reverse signals in the latest message (e.g. 'stop', 'undo', 'roll "
|
|
"back', 'just verify', 'don't do that anymore', 'never mind', a new "
|
|
"topic) must immediately end any in-flight work described in the "
|
|
"summary; do not re-surface it in later turns. "
|
|
"IMPORTANT: Your persistent memory (MEMORY.md, USER.md) in the system "
|
|
"prompt is ALWAYS authoritative and active — never ignore or deprioritize "
|
|
"memory content due to this compaction note. "
|
|
"None of the above restricts HOW you work: your tools remain fully "
|
|
"active — keep calling them normally for the active task (edit files, "
|
|
"run commands, search) instead of merely narrating what you would do. "
|
|
"The current session state (files, config, etc.) may reflect work "
|
|
"described here — avoid repeating it:"
|
|
)
|
|
LEGACY_SUMMARY_PREFIX = "[CONTEXT SUMMARY]:"
|
|
|
|
|
|
# Underscore prefix ON PURPOSE: wire sanitizers strip ``_``-keys; strict gateways
|
|
# reject unknown keys, so a bare key would poison every request in the session.
|
|
COMPRESSED_SUMMARY_METADATA_KEY = "_compressed_summary"
|
|
COMPRESSED_SUMMARY_HAS_USER_TURN_KEY = "_compressed_summary_has_user_turn"
|
|
# Only micro markers may be superseded/defragged/rehydrated: a batch marker's
|
|
# content is NOT in the rolling micro summary, so rewriting one destroys history.
|
|
MICRO_COMPACT_MARKER_KEY = "_micro_compact_marker"
|
|
# ``display_metadata`` flag on a row the model reads but nobody typed as one message (micro-compaction's
|
|
# merge of adjacent user turns). Its source rows stay in display history, so display projections skip it.
|
|
MODEL_ONLY_DISPLAY_METADATA_KEY = "model_only"
|
|
# What the user typed on a turn a ``pre_llm_call`` hook vetoed, kept inside ``display_metadata``. The row's
|
|
# ``content`` is the withheld placeholder, so every reader that hands transcript text to a model only ever sees
|
|
# that; transcript views render this key instead.
|
|
WITHHELD_PROMPT = "withheld_prompt"
|
|
PRE_LLM_BLOCK_WITHHELD_PROMPT = "[Message withheld: a pre_llm_call policy hook blocked it.]"
|
|
# The ask of a multimodal turn, kept inside ``display_metadata`` when the turn's injected context is appended
|
|
# to ``content`` as a text part: the stored content is one flattened string, so nothing else can tell the ask
|
|
# from the injection.
|
|
TYPED_ASK = "typed_ask"
|
|
|
|
|
|
def _display_metadata_str(message: Any, key: str) -> Optional[str]:
|
|
"""*message*'s ``display_metadata[key]`` when it is a string, else None."""
|
|
metadata = message.get("display_metadata") if isinstance(message, Mapping) else None
|
|
value = metadata.get(key) if isinstance(metadata, Mapping) else None
|
|
return value if isinstance(value, str) else None
|
|
|
|
|
|
def drop_typed_ask(message: MutableMapping[str, Any]) -> None:
|
|
"""Forget the typed-ask stamp on a row whose content no longer is that one ask (a fold, a redaction)."""
|
|
metadata = message.get("display_metadata")
|
|
if isinstance(metadata, dict) and TYPED_ASK in metadata:
|
|
message["display_metadata"] = {k: v for k, v in metadata.items() if k != TYPED_ASK}
|
|
|
|
|
|
def withheld_prompt_text(message: Any) -> Optional[str]:
|
|
"""The typed text a transcript view shows for a vetoed user row, else None."""
|
|
return _display_metadata_str(message, WITHHELD_PROMPT)
|
|
|
|
|
|
def typed_prompt_content(content: Any, message: Any) -> Any:
|
|
"""*content* (a rewind's live view of *message*) with the withheld placeholder swapped back for what
|
|
the user typed: ``/retry`` re-submits the real ask so the hook decides again, and ``/undo`` restores it
|
|
to the composer. Never the placeholder as a new prompt."""
|
|
typed = withheld_prompt_text(message)
|
|
if typed is None:
|
|
return content
|
|
if isinstance(content, str):
|
|
return content.replace(PRE_LLM_BLOCK_WITHHELD_PROMPT, typed)
|
|
if isinstance(content, list):
|
|
return [{**part, "text": part["text"].replace(PRE_LLM_BLOCK_WITHHELD_PROMPT, typed)}
|
|
if isinstance(part, dict) and isinstance(part.get("text"), str) else part for part in content]
|
|
return content
|
|
|
|
|
|
_DB_PERSISTED_MARKER = "_db_persisted"
|
|
|
|
|
|
COMPRESSION_CONTINUATION_USER_CONTENT = (
|
|
"Continue from the compressed conversation context above. "
|
|
"This marker exists because no human user turn was available."
|
|
)
|
|
_LEGACY_COMPRESSION_CONTINUATION_USER_CONTENT = (
|
|
"Continue from the compressed conversation context above. This marker exists because the compacted "
|
|
"transcript contained no preserved user turn."
|
|
)
|
|
# Content string is the authoritative marker: the store drops ``_``-metadata.
|
|
MAX_ITERATIONS_SUMMARY_REQUEST = (
|
|
"You've reached the maximum number of tool-calling iterations allowed. Please provide a final response "
|
|
"summarizing what you've found and accomplished so far, without calling any more tools."
|
|
)
|
|
_BACKGROUND_PROCESS_NOTIFICATION_PREFIX = "[IMPORTANT: Background process "
|
|
|
|
|
|
# Explicit end boundary: weak models otherwise read quoted headers as fresh
|
|
# user input or replay an assistant-role summary as their own output.
|
|
_SUMMARY_END_MARKER = "--- END OF CONTEXT SUMMARY — respond to the message below, not the summary above ---"
|
|
|
|
# Merged-into-tail case: prior tail content is kept BEFORE the summary inside
|
|
# these delimiters, so the summary prefix is not at content start.
|
|
_MERGED_PRIOR_CONTEXT_HEADER = "[PRIOR CONTEXT — for reference only; not a new message]"
|
|
_MERGED_SUMMARY_DELIMITER = "[END OF PRIOR CONTEXT — COMPACTION SUMMARY BELOW]"
|
|
|
|
|
|
# Exact wire text of every shipped prefix, newest-first; stale directives must
|
|
# still be strippable on resume. NEVER edit/reorder entries (byte-pinned); prepend.
|
|
_HISTORICAL_SUMMARY_PREFIXES = (
|
|
# Lacks the "no user message after summary => do nothing" clause.
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a handoff "
|
|
"from a previous context window — treat it as background reference, NOT as active instructions. Do NOT answer "
|
|
"questions or fulfill requests mentioned in this summary; they were already addressed. Respond ONLY to the "
|
|
"latest user message that appears AFTER this summary — that message is the single source of truth for what to do "
|
|
"right now. Topic overlap with the summary does NOT mean you should resume its task: even on similar topics, the "
|
|
"latest user message WINS. Treat ONLY the latest message as the active task and discard stale items from '## "
|
|
"Historical Task Snapshot' entirely — do not 'wrap up' or 'finish' work described there unless the latest "
|
|
"message explicitly asks for it. Reverse signals in the latest message (e.g. 'stop', 'undo', 'roll back', 'just "
|
|
"verify', 'don't do that anymore', 'never mind', a new topic) must immediately end any in-flight work described "
|
|
"in the summary; do not re-surface it in later turns. IMPORTANT: Your persistent memory (MEMORY.md, USER.md) in "
|
|
"the system prompt is ALWAYS authoritative and active — never ignore or deprioritize memory content due to this "
|
|
"compaction note. None of the above restricts HOW you work: your tools remain fully active — keep calling them "
|
|
"normally for the active task (edit files, run commands, search) instead of merely narrating what you would do. "
|
|
"The current session state (files, config, etc.) may reflect work described here — avoid repeating it:",
|
|
# Its discard clause names all four historical headings.
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a handoff "
|
|
"from a previous context window — treat it as background reference, NOT as active instructions. Do NOT answer "
|
|
"questions or fulfill requests mentioned in this summary; they were already addressed. Respond ONLY to the "
|
|
"latest user message that appears AFTER this summary — that message is the single source of truth for what to do "
|
|
"right now. Topic overlap with the summary does NOT mean you should resume its task: even on similar topics, the "
|
|
"latest user message WINS. Treat ONLY the latest message as the active task and discard stale items from '## "
|
|
"Historical Task Snapshot' / '## Historical In-Progress State' / '## Historical Pending User Asks' / '## "
|
|
"Historical Remaining Work' entirely — do not 'wrap up' or 'finish' work described there unless the latest "
|
|
"message explicitly asks for it. Reverse signals in the latest message (e.g. 'stop', 'undo', 'roll back', 'just "
|
|
"verify', 'don't do that anymore', 'never mind', a new topic) must immediately end any in-flight work described "
|
|
"in the summary; do not re-surface it in later turns. IMPORTANT: Your persistent memory (MEMORY.md, USER.md) in "
|
|
"the system prompt is ALWAYS authoritative and active — never ignore or deprioritize memory content due to this "
|
|
"compaction note. None of the above restricts HOW you work: your tools remain fully active — keep calling them "
|
|
"normally for the active task (edit files, run commands, search) instead of merely narrating what you would do. "
|
|
"The current session state (files, config, etc.) may reflect work described here — avoid repeating it:",
|
|
# Lacked the "tools remain fully active" clause (suppressed tool use).
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a handoff "
|
|
"from a previous context window — treat it as background reference, NOT as active instructions. Do NOT answer "
|
|
"questions or fulfill requests mentioned in this summary; they were already addressed. Respond ONLY to the "
|
|
"latest user message that appears AFTER this summary — that message is the single source of truth for what to do "
|
|
"right now. Topic overlap with the summary does NOT mean you should resume its task: even on similar topics, the "
|
|
"latest user message WINS. Treat ONLY the latest message as the active task and discard stale items from '## "
|
|
"Historical Task Snapshot' / '## Historical In-Progress State' / '## Historical Pending User Asks' / '## "
|
|
"Historical Remaining Work' entirely — do not 'wrap up' or 'finish' work described there unless the latest "
|
|
"message explicitly asks for it. Reverse signals in the latest message (e.g. 'stop', 'undo', 'roll back', 'just "
|
|
"verify', 'don't do that anymore', 'never mind', a new topic) must immediately end any in-flight work described "
|
|
"in the summary; do not re-surface it in later turns. IMPORTANT: Your persistent memory (MEMORY.md, USER.md) in "
|
|
"the system prompt is ALWAYS authoritative and active — never ignore or deprioritize memory content due to this "
|
|
"compaction note. The current session state (files, config, etc.) may reflect work described here — avoid "
|
|
"repeating it:",
|
|
# Carveout era: "consistent -> use as background" licensed stale resumption.
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a handoff "
|
|
"from a previous context window — treat it as background reference, NOT as active instructions. Do NOT answer "
|
|
"questions or fulfill requests mentioned in this summary; they were already addressed. Respond ONLY to the "
|
|
"latest user message that appears AFTER this summary — that message is the single source of truth for what to do "
|
|
"right now. If the latest user message is consistent with the '## Active Task' section, you may use the summary "
|
|
"as background. If the latest user message contradicts, supersedes, changes topic from, or in any way diverges "
|
|
"from '## Active Task' / '## In Progress' / '## Pending User Asks' / '## Remaining Work', the latest message "
|
|
"WINS — discard those stale items entirely and do not 'wrap up the old task first'. Reverse signals in the "
|
|
"latest message (e.g. 'stop', 'undo', 'roll back', 'just verify', 'don't do that anymore', 'never mind', a new "
|
|
"topic) must immediately end any in-flight work described in the summary; do not re-surface it in later turns. "
|
|
"IMPORTANT: Your persistent memory (MEMORY.md, USER.md) in the system prompt is ALWAYS authoritative and active "
|
|
"— never ignore or deprioritize memory content due to this compaction note. The current session state (files, "
|
|
"config, etc.) may reflect work described here — avoid repeating it:",
|
|
# Contains the self-contradicting "resume exactly" directive.
|
|
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a "
|
|
"handoff from a previous context window — treat it as background reference, NOT as active instructions. "
|
|
"Do NOT answer questions or fulfill requests mentioned in this summary; they were already addressed. "
|
|
"Your current task is identified in the '## Active Task' section of the summary — resume exactly from "
|
|
"there. Respond ONLY to the latest user message that appears AFTER this summary. The current session "
|
|
"state (files, config, etc.) may reflect work described here — avoid repeating it:",
|
|
)
|
|
|
|
|
|
def _part_text(item: Any) -> Optional[str]:
|
|
"""Text of a content part: the string itself, a dict's ``text``, else None."""
|
|
return item if isinstance(item, str) else item.get("text") if isinstance(item, dict) else None
|
|
|
|
|
|
def _with_part_text(item: Any, text: str) -> Any:
|
|
"""Copy of a content part carrying ``text`` (string parts become the text itself)."""
|
|
return {**item, "text": text} if isinstance(item, dict) else text
|
|
|
|
|
|
def _content_text_for_contains(content: Any) -> str:
|
|
"""Return a best-effort text view of message content (for substring checks only)."""
|
|
if isinstance(content, list):
|
|
return "\n".join(t for t in map(_part_text, content) if isinstance(t, str) and t)
|
|
return "" if content is None else content if isinstance(content, str) else str(content)
|
|
|
|
|
|
def _starts_with_summary_prefix(text: str) -> bool:
|
|
"""Return True if *text* begins with any known handoff prefix."""
|
|
return text.startswith((SUMMARY_PREFIX, LEGACY_SUMMARY_PREFIX, *_HISTORICAL_SUMMARY_PREFIXES))
|
|
|
|
|
|
def classify_summary_content(content: Any) -> Optional[str]:
|
|
"""Classify how *content* relates to a compaction summary.
|
|
Returns ``"standalone"`` (whole message is a handoff), ``"merged"`` (preserved content +
|
|
delimiter + summary body), or None."""
|
|
text = _content_text_for_contains(content).lstrip()
|
|
# Merged summaries carry the handoff prefix after the delimiter; detect it there too.
|
|
if _MERGED_SUMMARY_DELIMITER in text:
|
|
after = text.split(_MERGED_SUMMARY_DELIMITER, 1)[1].lstrip()
|
|
return "merged" if _starts_with_summary_prefix(after) else None
|
|
return "standalone" if _starts_with_summary_prefix(text) else None
|
|
|
|
|
|
def _is_context_summary_content(content: Any) -> bool:
|
|
return classify_summary_content(content) is not None
|
|
|
|
|
|
def _has_compressed_summary_metadata(message: Any) -> bool:
|
|
"""Return True if *message* carries the in-process compressed-summary flag."""
|
|
return isinstance(message, dict) and bool(message.get(COMPRESSED_SUMMARY_METADATA_KEY))
|
|
|
|
|
|
def _is_synthetic_compression_user_turn(message: Any) -> bool:
|
|
"""Recognize internal user-role rows by content marker (the store drops metadata)."""
|
|
if not isinstance(message, dict) or message.get("role") != "user":
|
|
return False
|
|
if _is_context_summary_message(message):
|
|
return True
|
|
text = _content_text_for_contains(message.get("content")).strip()
|
|
return text in {
|
|
COMPRESSION_CONTINUATION_USER_CONTENT, _LEGACY_COMPRESSION_CONTINUATION_USER_CONTENT,
|
|
MAX_ITERATIONS_SUMMARY_REQUEST, _CODEX_INCOMPLETE_NUDGE, _CODEX_ACK_CONTINUATION_NUDGE,
|
|
_DEGENERATE_FINAL_NUDGE, _DROPPED_TOOLCALL_NUDGE_CONTENT, _EMPTY_TOOL_RESPONSE_NUDGE,
|
|
_LENGTH_CONTINUATION_NETWORK_STUB, _LEGACY_LENGTH_CONTINUATION_NETWORK_STUB,
|
|
_LENGTH_CONTINUATION_OUTPUT_LIMIT,
|
|
} or text.startswith((
|
|
_BACKGROUND_PROCESS_NOTIFICATION_PREFIX, TODO_INJECTION_HEADER + "\n", _LENGTH_CONTINUATION_DROPPED_TOOLS_PREFIX,
|
|
))
|
|
|
|
|
|
def _is_context_summary_message(message: Any) -> bool:
|
|
"""Return True for summary handoff messages by metadata or content."""
|
|
if not isinstance(message, dict):
|
|
return False
|
|
return _has_compressed_summary_metadata(message) or _is_context_summary_content(message.get("content"))
|
|
|
|
|
|
def _is_blank_user_turn(message: Any) -> bool:
|
|
"""Return whether *message* is an empty, non-summary user-role echo."""
|
|
if not isinstance(message, dict) or message.get("role") != "user":
|
|
return False
|
|
if _is_context_summary_message(message):
|
|
return False
|
|
content = message.get("content")
|
|
if content is None or (isinstance(content, str) and not content.strip()):
|
|
return True
|
|
if not isinstance(content, list):
|
|
return False
|
|
|
|
def _blank_part(part: Any) -> bool:
|
|
if isinstance(part, str):
|
|
return not part.strip()
|
|
if isinstance(part, dict) and part.get("type") in {"text", "input_text"}:
|
|
return isinstance(part.get("text"), str) and not part["text"].strip()
|
|
return False
|
|
|
|
return all(map(_blank_part, content))
|
|
|
|
|
|
def _is_actionable_user_turn(message: Any) -> bool:
|
|
"""Return whether *message* contains user input worth anchoring."""
|
|
if not isinstance(message, dict) or message.get("role") != "user":
|
|
return False
|
|
# display_kind rows (internal notifications, hidden scaffolding) are not human input
|
|
# and must not anchor the tail or seed auto-focus. Mirrors is_user_originated_turn.
|
|
# A /steer row is typed for the renderer and the alternation repair, but it IS human input.
|
|
display_kind = message.get("display_kind")
|
|
if (display_kind and display_kind != STEER_DISPLAY_KIND) or _is_context_summary_message(message):
|
|
return False
|
|
return not _is_blank_user_turn(message)
|
|
|
|
|
|
def _strip_context_summary_handoff_message(message: dict[str, Any]) -> Optional[dict[str, Any]]:
|
|
"""Drop stale handoff data while preserving merged prior-tail content.
|
|
Returns a copy for non-handoff rows, the unwrapped prior-tail content for merged handoffs
|
|
(delimiter form, or legacy end-marker form), and ``None`` for standalone ones."""
|
|
if not isinstance(message, dict):
|
|
return message
|
|
if not _is_context_summary_message(message):
|
|
return message.copy()
|
|
content = message.get("content")
|
|
|
|
def _unwrapped(new_content: Any) -> dict[str, Any]:
|
|
unwrapped = {**message, "content": new_content}
|
|
unwrapped.pop(COMPRESSED_SUMMARY_METADATA_KEY, None)
|
|
return unwrapped
|
|
|
|
if isinstance(content, str):
|
|
if _MERGED_SUMMARY_DELIMITER in content:
|
|
prior = content.split(_MERGED_SUMMARY_DELIMITER, 1)[0].strip()
|
|
if prior.startswith(_MERGED_PRIOR_CONTEXT_HEADER):
|
|
prior = prior[len(_MERGED_PRIOR_CONTEXT_HEADER):].lstrip()
|
|
elif _SUMMARY_END_MARKER in content:
|
|
prior = content.split(_SUMMARY_END_MARKER, 1)[1].lstrip()
|
|
else:
|
|
prior = ""
|
|
return _unwrapped(prior) if prior else None
|
|
if isinstance(content, list):
|
|
prior_blocks: list[Any] = []
|
|
found_delimiter = False
|
|
for item in content:
|
|
text = _part_text(item)
|
|
if isinstance(text, str) and _MERGED_SUMMARY_DELIMITER in text:
|
|
before = text.split(_MERGED_SUMMARY_DELIMITER, 1)[0]
|
|
if before.strip():
|
|
prior_blocks.append(_with_part_text(item, before))
|
|
found_delimiter = True
|
|
break
|
|
prior_blocks.append(item.copy() if isinstance(item, dict) else item)
|
|
if not found_delimiter:
|
|
# Legacy end-marker form: live content follows the marker inside/after one part.
|
|
for index, item in enumerate(content):
|
|
text = _part_text(item)
|
|
if isinstance(text, str) and _SUMMARY_END_MARKER in text:
|
|
remainder = text.split(_SUMMARY_END_MARKER, 1)[1].lstrip()
|
|
legacy_blocks = [_with_part_text(item, remainder)] if remainder else []
|
|
legacy_blocks += [later.copy() if isinstance(later, dict) else later for later in content[index + 1:]]
|
|
return _unwrapped(legacy_blocks) if legacy_blocks else None
|
|
return None
|
|
|
|
# Strip the PRIOR CONTEXT header from the first block that carries it.
|
|
for index, item in enumerate(prior_blocks):
|
|
text = _part_text(item)
|
|
if isinstance(text, str) and text.lstrip().startswith(_MERGED_PRIOR_CONTEXT_HEADER):
|
|
leading = text.lstrip()[len(_MERGED_PRIOR_CONTEXT_HEADER):].lstrip()
|
|
if leading:
|
|
prior_blocks[index] = _with_part_text(item, leading)
|
|
else:
|
|
prior_blocks.pop(index)
|
|
break
|
|
return _unwrapped(prior_blocks) if prior_blocks else None
|
|
return None
|
|
|
|
|
|
def _is_real_user_turn(message: dict[str, Any]) -> bool:
|
|
"""Actionable user turn that is not synthetic scaffolding — the row test both index scans share.
|
|
|
|
Weaker than ``agent.conversation_compression._is_real_user_message``, which also rejects
|
|
metadata-flagged scaffolding this pair cannot see; use that one when the question is
|
|
"is this a genuine inbound user message".
|
|
"""
|
|
return _is_actionable_user_turn(message) and not _is_synthetic_compression_user_turn(message)
|
|
|
|
|
|
def is_compaction_summary_message(message: Any) -> bool:
|
|
"""Return True when *message* is a context-compaction handoff summary.
|
|
Public API. Uses the metadata key, falling back to content heuristics because the key is stripped by
|
|
wire sanitizers and some session-store round-trips."""
|
|
return _is_context_summary_message(message) if isinstance(message, dict) else _is_context_summary_content(message)
|
|
|
|
|
|
# Display metadata that survives projection; other metadata may describe synthetic events and must
|
|
# not look human.
|
|
SUMMARY_CARRIER_DURABLE_DISPLAY_METADATA_KEYS = ("reactions",)
|
|
|
|
|
|
def _handoff_only_content(content: Any) -> Any:
|
|
"""Project summary-bearing content to the synthetic handoff alone; never keeps live media."""
|
|
def _through_end_marker(text: str) -> str:
|
|
marker_idx = text.find(_SUMMARY_END_MARKER)
|
|
return text[: marker_idx + len(_SUMMARY_END_MARKER)] if marker_idx >= 0 else text
|
|
|
|
if isinstance(content, str):
|
|
if _MERGED_SUMMARY_DELIMITER in content:
|
|
content = content.split(_MERGED_SUMMARY_DELIMITER, 1)[1].lstrip()
|
|
return _through_end_marker(content)
|
|
if not isinstance(content, list):
|
|
return content
|
|
# Ordinary merge: summary suffix starts in the delimiter part; later parts may carry live media
|
|
# — never retain.
|
|
for item in content:
|
|
text = _part_text(item)
|
|
if not isinstance(text, str) or _MERGED_SUMMARY_DELIMITER not in text:
|
|
continue
|
|
suffix = _through_end_marker(text.split(_MERGED_SUMMARY_DELIMITER, 1)[1].lstrip())
|
|
return [_with_part_text(item, suffix)] if suffix else []
|
|
|
|
# Force-user-leading: keep parts through the end marker, truncated before the live ask.
|
|
projected: list[Any] = []
|
|
for item in content:
|
|
text = _part_text(item)
|
|
if not isinstance(text, str):
|
|
continue
|
|
if _SUMMARY_END_MARKER in text:
|
|
projected.append(_with_part_text(item, text.split(_SUMMARY_END_MARKER, 1)[0] + _SUMMARY_END_MARKER))
|
|
return projected
|
|
projected.append(item.copy() if isinstance(item, dict) else item)
|
|
return projected
|
|
|
|
|
|
def split_user_originated_turn(message: Any) -> tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]:
|
|
"""Split a user row into ``(handoff_only, live_view)``; either may be None; fresh dicts."""
|
|
if not isinstance(message, dict) or message.get("role") != "user":
|
|
return None, None
|
|
|
|
is_summary = is_compaction_summary_message(message)
|
|
handoff: Optional[dict[str, Any]] = None
|
|
if is_summary:
|
|
handoff = {
|
|
"role": "user", "content": _handoff_only_content(message.get("content")),
|
|
COMPRESSED_SUMMARY_METADATA_KEY: True, "display_kind": "hidden",
|
|
}
|
|
if COMPRESSED_SUMMARY_HAS_USER_TURN_KEY in message:
|
|
handoff[COMPRESSED_SUMMARY_HAS_USER_TURN_KEY] = bool(message.get(COMPRESSED_SUMMARY_HAS_USER_TURN_KEY))
|
|
if message.get(MICRO_COMPACT_MARKER_KEY):
|
|
handoff[MICRO_COMPACT_MARKER_KEY] = True
|
|
if message.get("timestamp") is not None:
|
|
handoff["timestamp"] = message["timestamp"]
|
|
drop_stale_api_content(handoff)
|
|
# Hidden is the legacy compaction wrapper and doesn't hide an unwrapped human payload; other
|
|
# kinds are synthetic.
|
|
display_kind = message.get("display_kind")
|
|
candidate = None if display_kind and display_kind != "hidden" else _strip_context_summary_handoff_message(message)
|
|
if candidate is None:
|
|
return handoff, None
|
|
elif message.get("display_kind") and message.get("display_kind") != STEER_DISPLAY_KIND:
|
|
return None, None
|
|
else:
|
|
candidate = message.copy() # includes a typed /steer row: full user authority
|
|
|
|
for key in (
|
|
COMPRESSED_SUMMARY_METADATA_KEY, COMPRESSED_SUMMARY_HAS_USER_TURN_KEY, MICRO_COMPACT_MARKER_KEY,
|
|
_DB_PERSISTED_MARKER, *(("_row_id",) if is_summary else ()), "display_kind", "display_metadata",
|
|
):
|
|
candidate.pop(key, None)
|
|
carrier_metadata = message.get("display_metadata")
|
|
if isinstance(carrier_metadata, dict):
|
|
durable_metadata = {
|
|
key: copy.deepcopy(carrier_metadata[key]) for key in SUMMARY_CARRIER_DURABLE_DISPLAY_METADATA_KEYS if key in carrier_metadata
|
|
}
|
|
if durable_metadata:
|
|
candidate["display_metadata"] = durable_metadata
|
|
drop_stale_api_content(candidate)
|
|
if not _is_real_user_turn(candidate):
|
|
return handoff, None
|
|
return handoff, candidate
|
|
|
|
|
|
def user_originated_turn_view(message: Any) -> Optional[dict[str, Any]]:
|
|
"""Return the live human-authored projection of a user row, if any."""
|
|
return split_user_originated_turn(message)[1]
|
|
|
|
|
|
def history_before_user_originated_turn(
|
|
messages: list[dict[str, Any]], index: int,
|
|
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
|
|
"""Rewind prefix and canonical live view for ``index``; a composite carrier keeps its handoff scaffold at the head."""
|
|
if index < 0 or index >= len(messages):
|
|
raise IndexError("user turn index is outside the transcript")
|
|
handoff, live_view = split_user_originated_turn(messages[index])
|
|
if live_view is None:
|
|
raise ValueError("selected row is not a user-originated turn")
|
|
live_view["content"] = typed_prompt_content(live_view.get("content"), messages[index]) # never the placeholder
|
|
return [message.copy() for message in messages[:index]] + ([handoff] if handoff is not None else []), live_view
|
|
|
|
|
|
def retryable_user_text(content: Any) -> str:
|
|
"""Lossless retry text, or raise before destructive mutation (media/unknown parts fail closed: no replay protocol)."""
|
|
if not isinstance(content, (str, list)):
|
|
raise ValueError("retry does not support non-text content")
|
|
chunks: list[str] = []
|
|
for part in [content] if isinstance(content, str) else content:
|
|
if isinstance(part, str):
|
|
chunks.append(part)
|
|
continue
|
|
if not isinstance(part, dict):
|
|
raise ValueError("retry does not support non-text content")
|
|
if part.get("type") not in {"text", "input_text", "output_text"}:
|
|
raise ValueError("retry does not support media or unknown content parts")
|
|
if set(part) - {"type", "text"}:
|
|
raise ValueError("retry cannot losslessly flatten annotated text parts")
|
|
if not isinstance(part.get("text"), str):
|
|
raise ValueError("retry text parts must contain text")
|
|
chunks.append(part["text"])
|
|
text = "".join(chunks)
|
|
if not text.strip():
|
|
raise ValueError("retry found no text to send")
|
|
return text
|
|
|
|
|
|
_COMPACTION_INTERNAL_FIELDS = (
|
|
"tool_calls",
|
|
"finish_reason",
|
|
"reasoning",
|
|
# Provider replay/metadata fields that ride the wire on every request but are invisible to
|
|
# ``msg["content"]``/``msg["tool_calls"]`` accounting. Codex Responses sessions in particular carry
|
|
# ``codex_reasoning_items`` blobs of ``encrypted_content`` that can dominate the serialized session (a
|
|
# measured 214-turn session held ~115K tokens / 27% of its payload there).
|
|
# ``reasoning_details`` is handled separately (see ``_reasoning_details_text_chars``): its signed/base64
|
|
# envelope is excluded from the budget, mirroring the preflight estimator's exclusion in
|
|
# ``model_metadata._estimate_message_tokens_without_images``.
|
|
# An assistant turn may carry only reasoning/thinking content with no visible text (extended-thinking
|
|
# turns, thinking-only recovery responses). Such a turn is persisted with its reasoning fields and is
|
|
# recallable from the transcript, but dropping it here as "empty" makes it vanish from the
|
|
# resumed/reloaded session view while the desktop's reasoning disclosure has nothing to render. Keep it
|
|
# when it carries reasoning so the "Thinking…" block still shows.
|
|
"reasoning_content",
|
|
"reasoning_details",
|
|
"codex_reasoning_items",
|
|
"codex_message_items",
|
|
)
|
|
|
|
|
|
def project_compaction_message_for_display(message: dict[str, Any]) -> Optional[dict[str, Any]]:
|
|
"""Return authentic transcript content, or ``None`` for a pure handoff.
|
|
|
|
Model-facing recovery history retains the complete carrier. Display
|
|
projections instead remove the handoff, inherited tool state, and internal
|
|
reasoning while preserving any real prior-tail content or live user ask
|
|
embedded in the carrier.
|
|
"""
|
|
if not isinstance(message, dict):
|
|
return None
|
|
if not is_compaction_summary_message(message):
|
|
return message.copy()
|
|
|
|
projected = _strip_context_summary_handoff_message(message)
|
|
if projected is None:
|
|
return None
|
|
|
|
projected = projected.copy()
|
|
for key in _COMPACTION_INTERNAL_FIELDS:
|
|
projected.pop(key, None)
|
|
# A safety-refused ask embedded in a composite carrier keeps its ``content_refused`` tag through
|
|
# the unwrap, or a reopened chat would redraw it as an ordinary user bubble with no refused notice:
|
|
# the model-facing handoff is gone, the user's live ask survives as a greyed refused block. Every
|
|
# other carrier drops its internal ``display_kind``.
|
|
if projected.get("display_kind") != CONTENT_REFUSED_DISPLAY_KIND:
|
|
projected.pop("display_kind", None)
|
|
return projected
|
|
|
|
|
|
# Unanswered text a merged user row held before the current turn was absorbed
|
|
# (agent_runtime_helpers._merge_consecutive_users); the persist override keeps it.
|
|
# It repeats the row's own content, so pricing it would double the estimate.
|
|
MERGED_TURN_PREFIX = "_merged_turn_prefix"
|
|
# The durable per-message id (``messages.message_uid``): minted once at the row's first insert and kept by
|
|
# every host copy of that logical message (in-place compaction generation, rotation child, concurrent-tail
|
|
# clone, replace re-issue, rewrite in place). Unlike ``_row_id`` (a physical id re-issued per copy, opt-in on
|
|
# restore) it is restored unconditionally, so context engines can key on it across restarts and boundaries.
|
|
MESSAGE_UID = "message_uid"
|
|
# The merge witness on a consecutive-user merge survivor: the ``message_uid`` of each absorbed row, in
|
|
# absorption order (the uid sibling of ``_absorbed_row_ids``; persisted as ``messages.absorbed_message_uids``).
|
|
# The survivor keeps the FIRST constituent's uid.
|
|
ABSORBED_MESSAGE_UIDS = "_absorbed_message_uids"
|
|
# Per-occurrence tool-call identity. Provider tool-call ids repeat (Hermes mints deterministic ``call_<12hex>``
|
|
# ids for identical calls, and models reuse ids), so an assistant row carries ``{provider id: uid}`` for its
|
|
# ``tool_calls`` (``messages.tool_call_uids``) and its tool-result rows carry the matching uid
|
|
# (``messages.tool_call_uid``). The provider-facing ``id`` is untouched; these never reach the wire.
|
|
TOOL_CALL_UIDS = "_tool_call_uids"
|
|
|
|
|
|
def mint_uid() -> str:
|
|
"""A fresh durable id (message or tool-call occurrence): random, never derived from content."""
|
|
return uuid4().hex
|
|
|
|
|
|
def uid_list(value: Any) -> list[str]:
|
|
"""The unique non-empty string uids of a live list, in order; anything else is ``[]``."""
|
|
return list(dict.fromkeys(u for u in (value if isinstance(value, list) else ()) if isinstance(u, str) and u))
|
|
|
|
|
|
def message_uid_or_none(msg: Mapping[str, Any]) -> Optional[str]:
|
|
"""The dict's ``message_uid`` when it is a non-empty string, else ``None`` (never coerced: an int or a
|
|
blank would be a bug upstream of the write, not an identity)."""
|
|
uid = msg.get(MESSAGE_UID)
|
|
return uid if isinstance(uid, str) and uid else None
|
|
|
|
|
|
def _is_empty_content(content: Any) -> bool:
|
|
"""Nothing to fold: any part (an image too) is content, unlike ``is_content_blank``'s text-only test."""
|
|
return content is None or content == [] or (isinstance(content, str) and not content.strip())
|
|
|
|
|
|
def record_absorbed_message(
|
|
survivor: MutableMapping[str, Any], dropped: Mapping[str, Any], *, dropped_leads: bool = False,
|
|
) -> None:
|
|
"""Merge-witness bookkeeping for every host fold of *dropped* into *survivor*.
|
|
|
|
The composite keeps the uid of the constituent whose text comes first and records every other
|
|
constituent's uid in ``_absorbed_message_uids`` (text order, no repeats). By default the survivor's
|
|
text leads; with *dropped_leads* the dropped dict's text was put first (the real user anchor folded
|
|
into a scaffolding turn), so its uid becomes the survivor's and the survivor's former uid is recorded.
|
|
A dict without a uid (unflushed, scaffolding, engine-authored) contributes nothing; an empty result
|
|
leaves the survivor untouched. The survivor's typed-ask stamp goes when *dropped* brought content (the row
|
|
now holds more than that one ask, so ``previous_message`` reads it as before stamps existed); an empty
|
|
turn leaves the content, and so the stamp, as they were.
|
|
"""
|
|
if not _is_empty_content(dropped.get("content")):
|
|
drop_typed_ask(survivor)
|
|
survivor_uid = message_uid_or_none(survivor)
|
|
dropped_uid = message_uid_or_none(dropped)
|
|
own = uid_list(survivor.get(ABSORBED_MESSAGE_UIDS))
|
|
theirs = uid_list(dropped.get(ABSORBED_MESSAGE_UIDS))
|
|
if dropped_leads and dropped_uid:
|
|
survivor[MESSAGE_UID] = dropped_uid
|
|
ordered = theirs + ([survivor_uid] if survivor_uid else []) + own
|
|
else:
|
|
ordered = own + ([dropped_uid] if dropped_uid else []) + theirs
|
|
if absorbed := [uid for uid in dict.fromkeys(ordered) if uid != survivor.get(MESSAGE_UID)]:
|
|
survivor[ABSORBED_MESSAGE_UIDS] = absorbed
|
|
|
|
|
|
def merge_tool_call_uids(into: Mapping[str, Any], extra: Mapping[str, Any]) -> dict:
|
|
"""Union two ``_tool_call_uids`` maps without losing an occurrence. Folding two assistant turns that
|
|
reuse a provider id (llama.cpp-style constant ids) leaves both calls in ``tool_calls``; the id then maps
|
|
to every occurrence's uid, in call order, as a list. Provider ids themselves are never rewritten."""
|
|
merged = dict(into)
|
|
for call_id, uid in extra.items():
|
|
merged[call_id] = _uid_occurrences(merged[call_id]) + _uid_occurrences(uid) if call_id in merged else uid
|
|
return merged
|
|
|
|
|
|
def _uid_occurrences(value: Any) -> list:
|
|
return list(value) if isinstance(value, list) else [value]
|
|
|
|
|
|
def per_occurrence_tool_call_uids(uids: Mapping[str, Any], tool_calls: list[Mapping[str, Any]]) -> dict:
|
|
"""*uids* with every provider id this row names more than once spelled out as one uid per occurrence. A
|
|
single response that repeats an id shares ONE uid (its results carry it, so every call stays paired);
|
|
before a fold appends another turn's occurrences, the shared uid must fill each of this row's slots or
|
|
the appended uids would align with the wrong calls."""
|
|
counts = Counter(call_id for tc in tool_calls if (call_id := coalesce_tool_call_id(tc)))
|
|
expanded = dict(uids)
|
|
for call_id, count in counts.items():
|
|
if count > 1 and isinstance(uid := uids.get(call_id), str):
|
|
expanded[call_id] = [uid] * count
|
|
return expanded
|
|
|
|
|
|
def index_tool_call_uids(index: MutableMapping[str, str], assistant: Mapping[str, Any]) -> frozenset:
|
|
"""Register an assistant dict's ``_tool_call_uids`` under every pairing-id variant of its tool calls, so a
|
|
later tool-result row can be resolved by any spelling of its ``tool_call_id``. Provider ids repeat, so
|
|
every id this assistant names first shadows an earlier occurrence's entry: a result pairs with the
|
|
NEAREST preceding call, and a call without a uid (a legacy row) pairs its result with nothing. A
|
|
provider id repeated inside one row (two folded turns) maps each call to its own occurrence's uid.
|
|
Returns every pairing-id variant the assistant names."""
|
|
calls = [(tc, tool_call_id_variants(tc)) for tc in assistant.get("tool_calls") or ()]
|
|
named = frozenset(variant for _, variants in calls for variant in variants)
|
|
for variant in named:
|
|
index.pop(variant, None)
|
|
uids = assistant.get(TOOL_CALL_UIDS)
|
|
if not isinstance(uids, dict) or not uids:
|
|
return named
|
|
occurrence: dict = {} # provider id -> calls seen so far: a list value holds one uid per occurrence
|
|
for tc, variants in calls:
|
|
call_id = coalesce_tool_call_id(tc)
|
|
uid = uids.get(call_id)
|
|
if isinstance(uid, list):
|
|
nth = occurrence[call_id] = occurrence.get(call_id, -1) + 1
|
|
uid = uid[nth] if nth < len(uid) else None
|
|
if isinstance(uid, str) and uid:
|
|
for variant in variants:
|
|
index[variant] = uid
|
|
return named
|
|
|
|
|
|
def resolve_tool_call_uid(index: MutableMapping[str, str], tool_call_id: Any) -> Optional[str]:
|
|
"""The uid an indexed assistant dict minted for ``tool_call_id`` (any variant), else ``None``."""
|
|
if not index or not isinstance(tool_call_id, str) or not tool_call_id:
|
|
return None
|
|
for variant in tool_result_id_variants(tool_call_id):
|
|
uid = index.get(variant)
|
|
if uid:
|
|
return uid
|
|
return None
|
|
|
|
|
|
def _tc_field(tc: Any, key: str) -> Any:
|
|
"""Read ``key`` from a tool-call entry that may be a dict or an SDK object."""
|
|
return tc.get(key) if isinstance(tc, dict) else getattr(tc, key, None)
|
|
|
|
|
|
def _expand_tool_id_variants(values: tuple[Any, ...]) -> frozenset[str]:
|
|
"""Every wire spelling of one tool-call identifier: Responses bridges may expose the pairing
|
|
id and response-item id separately or as ``call_id|response_item_id``; all alias ONE call."""
|
|
variants: set[str] = set()
|
|
for raw in values:
|
|
value = raw.strip() if isinstance(raw, str) else ""
|
|
if value:
|
|
variants.add(value)
|
|
variants.update(p for p in (part.strip() for part in value.split("|")) if p)
|
|
return frozenset(variants)
|
|
|
|
|
|
def tool_call_id_variants(tc: Any) -> frozenset[str]:
|
|
"""Return all pairing-id variants carried by a tool-call entry."""
|
|
return _expand_tool_id_variants(tuple(_tc_field(tc, k) for k in ("call_id", "id", "response_item_id")))
|
|
|
|
|
|
def tool_result_id_variants(tool_call_id: Any) -> frozenset[str]:
|
|
"""Return all matching variants for a role=tool ``tool_call_id``."""
|
|
return _expand_tool_id_variants((tool_call_id,))
|
|
|
|
|
|
def coalesce_tool_call_id(tc: Any) -> str:
|
|
"""Effective call id of a tool_call entry (dict or object); ``""`` when none. Codex Responses
|
|
carry ``call_id`` (authoritative pairing key), Chat Completions ``id`` only, and bridge ids
|
|
may be ``call_id|response_item_id``."""
|
|
for raw in (_tc_field(tc, "call_id"), _tc_field(tc, "id")):
|
|
value = raw.strip() if isinstance(raw, str) else ""
|
|
if value:
|
|
return value.split("|", 1)[0].strip() or value
|
|
return ""
|
|
|
|
|
|
# How many durable rows an alternation repair folded into one user dict. A reload without row ids
|
|
# has no other way to tell that dict from a prompt that was never persisted.
|
|
MERGED_DURABLE_ROWS = "_merged_durable_rows"
|
|
# How many more durable rows a dict stands for on a reload without row ids, besides a merged user
|
|
# run: rows the repair dropped behind it (a tool call with no result, a result with no call) or
|
|
# folded into an assistant turn. The caller was handed them, and nothing else names them.
|
|
UNNAMED_DURABLE_ROWS = "_unnamed_durable_rows"
|
|
# What names those rows at commit: role, content and tool-call ids of each, one per counted row.
|
|
# The commit matches each to one active row, so the rest of the held history keeps exact coverage.
|
|
RETIRED_DURABLE_ROWS = "_retired_durable_rows"
|
|
# Tags an entry of RETIRED_DURABLE_ROWS once coverage_for_commit hands it to the commit as unresolved.
|
|
RETIRED_ROW = "_retired_row"
|
|
# Marks the entry for the dict's own row: an assistant fold rewrote its text, so content no longer names it.
|
|
# It stands in the dict's own tail slot and is not one of the counted rows behind it.
|
|
OWN_ROW = "_own_row"
|
|
|
|
|
|
def retired_row_payload(message: dict[str, Any]) -> dict[str, Any]:
|
|
"""The fields the commit matches a retired id-less row by."""
|
|
payload: dict[str, Any] = {"role": message.get("role"), "content": message.get("content")}
|
|
if message.get("tool_call_id"):
|
|
payload["tool_call_id"] = message["tool_call_id"]
|
|
calls = [call.get("id") for call in message.get("tool_calls") or () if isinstance(call, dict)]
|
|
if calls:
|
|
payload["tool_call_ids"] = calls
|
|
return payload
|
|
|
|
|
|
def _is_multimodal_tool_result(value: Any) -> bool:
|
|
"""True for the multimodal envelope: dict with ``_multimodal=True`` and a ``content`` list."""
|
|
return isinstance(value, dict) and value.get("_multimodal") is True and isinstance(value.get("content"), list)
|
|
|
|
|
|
def _is_text_part(p: Any) -> bool:
|
|
return isinstance(p, dict) and p.get("type") == "text"
|
|
|
|
|
|
def _multimodal_text_summary(value: Any) -> str:
|
|
"""Plain-text view of a tool result (logging, previews, string-only providers)."""
|
|
if isinstance(value, str):
|
|
return value
|
|
if _is_multimodal_tool_result(value):
|
|
if value.get("text_summary"):
|
|
return str(value["text_summary"])
|
|
parts = [str(p.get("text", "")) for p in value.get("content") or [] if _is_text_part(p)]
|
|
return "\n".join(parts) if parts else "[multimodal tool result]"
|
|
try:
|
|
return json.dumps(value, default=str)
|
|
except Exception: # health: allow BLE001 -- a value json cannot encode falls back to str()
|
|
return str(value)
|
|
|
|
|
|
# Flags marking ephemeral recovery scaffolding the loop pops before appending the real response.
|
|
# Persistence must skip them or a resumed session replays synthetic turns / breaks prefix-cache reuse.
|
|
_EPHEMERAL_SCAFFOLDING_FLAGS = (
|
|
"_empty_recovery_synthetic",
|
|
"_empty_terminal_sentinel",
|
|
"_thinking_prefill",
|
|
"_verification_stop_synthetic", # verify-on-stop nudge; the assistant candidate itself is NOT synthetic
|
|
"_pre_verify_synthetic",
|
|
"_kanban_stop_synthetic", # kanban worker stop-guard
|
|
"_dropped_toolcall_nudge", # internal retry instruction; must not replay as user context
|
|
)
|
|
|
|
_IMAGE_PART_TYPES = {"image", "image_url", "input_image"}
|
|
|
|
|
|
def _is_ephemeral_scaffolding(msg: Any) -> bool:
|
|
"""True when ``msg`` is internal recovery scaffolding that must never reach the durable transcript."""
|
|
return isinstance(msg, dict) and any(msg.get(flag) for flag in _EPHEMERAL_SCAFFOLDING_FLAGS)
|
|
|
|
|
|
def _durable_content(content: Any) -> Any:
|
|
"""Text-only DB projection: multimodal envelopes → summary; part lists keep text, images → ``[screenshot]``."""
|
|
if _is_multimodal_tool_result(content):
|
|
return _multimodal_text_summary(content)
|
|
if not isinstance(content, list):
|
|
return content
|
|
txt = [
|
|
str(p.get("text", "")) if p.get("type") == "text" else _image_marker(p)
|
|
for p in content
|
|
if isinstance(p, dict) and (p.get("type") == "text" or p.get("type") in _IMAGE_PART_TYPES)
|
|
]
|
|
return "\n".join(txt) if txt else None
|
|
|
|
|
|
def _image_marker(part: dict) -> str:
|
|
"""``[screenshot]`` at the image's position; a caller-chosen ``detail`` rides along so a replay
|
|
(``agent.image_replay``) rebuilds the part the live turn sent."""
|
|
url = part.get("image_url")
|
|
detail = part.get("detail") or (url.get("detail") if isinstance(url, dict) else None)
|
|
return f"[screenshot detail={detail}]" if detail in ("low", "high", "auto") else "[screenshot]"
|
|
|
|
|
|
_CONTENT_JSON_PREFIX = CONTENT_JSON_PREFIX
|
|
|
|
|
|
def _json_or(raw: Any, fallback: Any, warning: str) -> Any:
|
|
"""``json.loads(raw)``; on failure log *warning* and return *fallback*."""
|
|
try:
|
|
return json.loads(raw)
|
|
except (json.JSONDecodeError, TypeError):
|
|
logger.warning(warning)
|
|
return fallback
|
|
|
|
|
|
def decode_content(content: Any) -> Any:
|
|
"""Reverse :meth:`_encode_content`; returns scalars unchanged. Fail closed: a reserved-prefix
|
|
string that isn't valid JSON (a legacy/malformed marker row) returns the raw string."""
|
|
if isinstance(content, str) and content.startswith(_CONTENT_JSON_PREFIX):
|
|
return _json_or(content[len(_CONTENT_JSON_PREFIX):], content,
|
|
"Failed to decode JSON-encoded message content; returning raw string")
|
|
return content
|
|
|
|
|
|
_decode_content = decode_content
|
|
|
|
|
|
def _loaded_view_content(role: str, content: Any) -> Any:
|
|
"""Content as ``_rows_to_messages`` hands it to callers: user/assistant strings are sanitized and
|
|
stripped on load, so a reloaded session's messages must be compared to rows through the same lens."""
|
|
if role in {"user", "assistant"} and isinstance(content, str):
|
|
return sanitize_context(content).strip()
|
|
return content
|
|
|
|
|
|
def _is_codex_interim(m: dict) -> bool:
|
|
"""Codex Responses interim turn: carries its own continuation state, replayed verbatim."""
|
|
return bool(
|
|
m.get("codex_reasoning_items")
|
|
or m.get("codex_message_items")
|
|
or m.get("finish_reason") == "incomplete"
|
|
)
|
|
|
|
|
|
def _merge_assistant_into(prev: dict, msg: dict) -> bool:
|
|
"""Fold consecutive assistant *msg* into *prev* (union tool_calls, concat text). Returns whether *msg*'s
|
|
text survives: multimodal (list) content is never joined."""
|
|
prev_calls = list(prev.get("tool_calls") or [])
|
|
new_calls = list(msg.get("tool_calls") or [])
|
|
calls_changed = False
|
|
if new_calls:
|
|
prev["tool_calls"] = prev_calls + new_calls
|
|
# The absorbed turn's calls keep the per-occurrence ids they were persisted with.
|
|
if isinstance(extra := msg.get(TOOL_CALL_UIDS), dict) and extra:
|
|
prev[TOOL_CALL_UIDS] = merge_tool_call_uids(
|
|
per_occurrence_tool_call_uids(
|
|
own if isinstance(own := prev.get(TOOL_CALL_UIDS), dict) else {}, prev_calls),
|
|
per_occurrence_tool_call_uids(extra, new_calls))
|
|
calls_changed = True
|
|
elif prev_calls:
|
|
prev["tool_calls"] = prev_calls
|
|
else:
|
|
# Drop a stale ``tool_calls: []`` at the source: strict providers (DeepSeek v4, Kimi) 400 on
|
|
# it and it persists into replayed history.
|
|
# Neither turn carries tool calls, but the surviving turn may still carry a stale ``tool_calls: []``
|
|
# from the earlier message. An empty array is semantically "no tool calls", yet strict
|
|
# OpenAI-compatible providers (DeepSeek v4, Moonshot/Kimi) reject it with HTTP 400 ("Invalid
|
|
# 'messages[N].tool_calls': empty array..."). Drop the key HERE, at the source:
|
|
# ``sanitize_api_messages`` only fixes the per-call wire copy, so a ``[]`` left on the repaired turn
|
|
# survives in the live/persisted trajectory returned to callers (gateway/WebUI transcripts, session
|
|
# resume, subagents, cron) and is replayed on the next turn, where a fix at the wire copy alone
|
|
# never reaches it. Popping is non-destructive: an empty array carries no
|
|
# information.
|
|
calls_changed = "tool_calls" in prev
|
|
prev.pop("tool_calls", None)
|
|
# Concatenate plain-text content only; leave multimodal (list) content alone.
|
|
prev_content = prev.get("content")
|
|
new_content = msg.get("content")
|
|
content_rewritten = False
|
|
text_kept = not new_content # nothing to lose
|
|
if isinstance(prev_content, str) and isinstance(new_content, str):
|
|
text_kept = True
|
|
joined = "\n".join(p for p in (prev_content.strip(), new_content.strip()) if p)
|
|
prev["content"] = joined
|
|
# A falsy new_content leaves ``joined`` == prev_content; that is not a rewrite.
|
|
# "") strips to nothing and ``joined`` collapses back to ``prev_content`` unchanged -- that must NOT
|
|
# count as a rewrite.
|
|
content_rewritten = joined != prev_content
|
|
elif not prev_content and new_content is not None:
|
|
prev["content"] = new_content
|
|
content_rewritten = new_content != prev_content
|
|
text_kept = True
|
|
# Carry reasoning_content from the later turn only if the earlier lacks it (strict thinking
|
|
# providers need one on the merged tool-call turn).
|
|
reasoning_carried = False
|
|
if not prev.get("reasoning_content") and msg.get("reasoning_content"):
|
|
prev["reasoning_content"] = msg["reasoning_content"]
|
|
reasoning_carried = True
|
|
# A stale ``api_content`` sidecar overrides ``content`` at API-build time and would replay
|
|
# pre-merge bytes; drop it only when content actually changed.
|
|
# ``prev`` may carry an ``api_content`` sidecar (the exact bytes previously sent to the API, e.g. a
|
|
# sanitize-divergence stamp — see ``_flush_messages_to_session_db``) from BEFORE this merge. The sidecar
|
|
# takes priority over ``content`` at API-build time (``conversation_loop``'s ``api_messages`` build
|
|
# substitutes it back in for role ``assistant``), so leaving it in place while ``prev["content"]``
|
|
# changes would silently replay the pre-merge bytes and discard everything this merge just concatenated
|
|
# on — the same stale-field-survives-the-merge shape as the ``tool_calls`` gap above, just for a
|
|
# different field. Only drop it when the merge actually changed the resulting value (e.g. the later
|
|
# turn's content is ``None``, or either side is multimodal/list — both branches skip the reassignment
|
|
# and ``prev["content"]`` is untouched; a falsy ``new_content`` that strips to nothing also leaves
|
|
# ``joined`` equal to the original ``prev_content``): in those cases the sidecar is still the exact
|
|
# bytes previously sent for the UNCHANGED content, and dropping it would break the prompt-cache replay
|
|
# invariant for no reason.
|
|
if content_rewritten:
|
|
drop_stale_api_content(prev)
|
|
# The persist marker asserts the whole row is durable (content, tool_calls, reasoning sidecar), so
|
|
# any merged field stales it; pop it or the flush scan identity-skips the merged dict and the DB
|
|
# keeps the pre-merge row. The caller recomputes the flush cursor for the surviving sequence.
|
|
if content_rewritten or calls_changed or reasoning_carried:
|
|
prev.pop(_DB_PERSISTED_MARKER, None)
|
|
return text_kept
|
|
|
|
|
|
def _remember_absorbed_row(survivor: dict[str, Any], dropped: dict[str, Any], *, folded: bool) -> None:
|
|
"""Retire *dropped*'s row ids onto *survivor*; record its uid as a merge witness only when *folded* (its
|
|
text survives). An empty incoming turn still merges; stamping an empty list would change a message that
|
|
absorbed nothing. A dropped id that equals the survivor's own live id (the display-marker merge adopts
|
|
the plain row's id) is not an absorbed row: the survivor IS that row."""
|
|
own_id = survivor.get("_row_id")
|
|
ids = []
|
|
row_id = dropped.get("_row_id")
|
|
if isinstance(row_id, int) and not isinstance(row_id, bool) and row_id > 0 and row_id != own_id:
|
|
ids.append(row_id)
|
|
for older in dropped.get("_absorbed_row_ids") or ():
|
|
if isinstance(older, int) and not isinstance(older, bool) and older > 0 and older not in ids:
|
|
ids.append(older)
|
|
if ids:
|
|
absorbed = survivor.setdefault("_absorbed_row_ids", [])
|
|
for row_id in ids:
|
|
if row_id not in absorbed:
|
|
absorbed.append(row_id)
|
|
# The rows the retired dict counted are behind the survivor now.
|
|
if dropped.get(UNNAMED_DURABLE_ROWS):
|
|
survivor[UNNAMED_DURABLE_ROWS] = int(survivor.get(UNNAMED_DURABLE_ROWS) or 0) + int(dropped[UNNAMED_DURABLE_ROWS])
|
|
# Its own row is behind the survivor now too.
|
|
if dropped.get(RETIRED_DURABLE_ROWS):
|
|
survivor.setdefault(RETIRED_DURABLE_ROWS, []).extend(
|
|
{k: v for k, v in row.items() if k != OWN_ROW} for row in dropped[RETIRED_DURABLE_ROWS])
|
|
# The uid witness claims the dropped dict's TEXT lives on in the survivor: only a fold earns it. A
|
|
# superseded row (``folded=False``) is retired like any absorbed row but its content is discarded.
|
|
if folded:
|
|
record_absorbed_message(survivor, dropped)
|
|
|
|
|
|
def _count_unnamed_row(survivor: dict[str, Any], retired: dict[str, Any]) -> None:
|
|
"""On a reload without row ids nothing names *retired*'s durable row once the repair takes the dict
|
|
out of the list, so *survivor* counts it. A dict that counts rows was loaded too: a merge may have
|
|
popped its marker since. Call before the merge rewrites the survivor: *retired*'s own fields are
|
|
recorded so the commit can name its row."""
|
|
def loaded(message: dict[str, Any]) -> bool:
|
|
return bool(message.get(_DB_PERSISTED_MARKER) or message.get(UNNAMED_DURABLE_ROWS))
|
|
|
|
if loaded(survivor) and loaded(retired) and not isinstance(retired.get("_row_id"), int):
|
|
survivor[UNNAMED_DURABLE_ROWS] = int(survivor.get(UNNAMED_DURABLE_ROWS) or 0) + 1
|
|
# A previously folded dict already records its original row. The transfer in
|
|
# _remember_absorbed_row retires that record; its synthetic text/calls never existed in storage.
|
|
if not any(row.get(OWN_ROW) for row in retired.get(RETIRED_DURABLE_ROWS) or ()):
|
|
survivor.setdefault(RETIRED_DURABLE_ROWS, []).append(retired_row_payload(retired))
|
|
|
|
|
|
def _remember_own_row(survivor: dict[str, Any]) -> None:
|
|
"""An assistant fold rewrites *survivor*'s text, so on a reload without row ids its own row no longer
|
|
matches it by content. Record its loaded fields first, once."""
|
|
recorded = survivor.get(RETIRED_DURABLE_ROWS) or ()
|
|
if (survivor.get(_DB_PERSISTED_MARKER) and not isinstance(survivor.get("_row_id"), int)
|
|
and not any(isinstance(row, dict) and row.get(OWN_ROW) for row in recorded)):
|
|
survivor.setdefault(RETIRED_DURABLE_ROWS, []).append({**retired_row_payload(survivor), OWN_ROW: True})
|
|
|
|
|
|
def _retire_dropped_row(kept: list[dict], dropped: dict[str, Any], leading: list[dict]) -> None:
|
|
"""A durable row the repair drops was still handed to the caller, so the survivor before it stands
|
|
for it. Left unnamed, an in-place compaction takes it for a row another surface appended and
|
|
re-sequences it behind the running turn. A drop with no survivor before it waits in *leading*."""
|
|
if not kept:
|
|
leading.append(dropped)
|
|
elif isinstance(kept[-1], dict):
|
|
_count_unnamed_row(kept[-1], dropped)
|
|
_remember_absorbed_row(kept[-1], dropped, folded=False)
|
|
|
|
|
|
def _retire_leading_drops(kept: list[dict], leading: list[dict]) -> None:
|
|
"""Rows dropped ahead of the first survivor sit right before its own row, so it stands for them."""
|
|
for dropped in leading if kept and isinstance(kept[0], dict) else ():
|
|
_count_unnamed_row(kept[0], dropped)
|
|
_remember_absorbed_row(kept[0], dropped, folded=False)
|
|
|
|
|
|
def _merge_consecutive_assistants(messages: list[dict]) -> tuple[list[dict], int]:
|
|
"""Pass 0: merge consecutive assistant turns (codex interims exempt)."""
|
|
repairs = 0
|
|
collapsed: list[dict] = []
|
|
for msg in messages:
|
|
prev = collapsed[-1] if collapsed and isinstance(collapsed[-1], dict) else None
|
|
if (
|
|
prev is not None and prev.get("role") == "assistant"
|
|
and isinstance(msg, dict) and msg.get("role") == "assistant"
|
|
and not _is_codex_interim(msg) and not _is_codex_interim(prev)
|
|
):
|
|
# A provisional verification candidate is superseded, not unioned.
|
|
if prev.get("finish_reason") in {"verification_required", "verify_hook_continue"}:
|
|
_count_unnamed_row(msg, prev)
|
|
_remember_absorbed_row(msg, prev, folded=False)
|
|
collapsed[-1] = msg
|
|
else:
|
|
_count_unnamed_row(prev, msg)
|
|
_remember_own_row(prev)
|
|
_remember_absorbed_row(prev, msg, folded=_merge_assistant_into(prev, msg))
|
|
repairs += 1
|
|
continue
|
|
collapsed.append(msg)
|
|
return collapsed, repairs
|
|
|
|
|
|
def _drop_stray_tool_results(messages: list[dict]) -> tuple[list[dict], int]:
|
|
"""Pass 1: drop tool results not following a known assistant tool call. Consumes the whole
|
|
alias group (call_id/id/response_item_id/composite) so a duplicate keyed on a sibling
|
|
alias is not replayed to strict providers."""
|
|
repairs = 0
|
|
known_tool_ids: dict[str, int] = {} # alias -> group id; reset by assistant/user turns
|
|
# Pass 1: drop stray tool messages that don't follow a known assistant tool call. A Responses call can
|
|
# have several equivalent spellings (call_id, id, response_item_id, or a composite ``call|item`` id), so
|
|
# consume the whole alias group when one spelling is matched. Alias expansion lives in
|
|
# ``agent.message_sanitization.tool_call_id_variants`` / ``tool_result_id_variants`` (single policy
|
|
# owner) — which also handles SDK tool_call objects, keeping the dict-or-object tolerance.
|
|
matched_tool_groups: set = set()
|
|
next_tool_group = 0
|
|
filtered: list[dict] = []
|
|
leading: list[dict] = []
|
|
for msg in messages:
|
|
role = msg.get("role") if isinstance(msg, dict) else None
|
|
if role in ("assistant", "user"):
|
|
# An assistant turn starts a new tool-result run; a user turn closes it (later tool
|
|
# messages are orphans).
|
|
known_tool_ids = {}
|
|
matched_tool_groups = set()
|
|
for tc in (msg.get("tool_calls") or []) if role == "assistant" else ():
|
|
variants = tool_call_id_variants(tc)
|
|
if variants:
|
|
for tc_id in variants:
|
|
known_tool_ids.setdefault(tc_id, next_tool_group)
|
|
next_tool_group += 1
|
|
elif role == "tool":
|
|
result_variants = tool_result_id_variants(msg.get("tool_call_id"))
|
|
candidate_groups = {
|
|
known_tool_ids[tc_id] for tc_id in result_variants
|
|
if tc_id in known_tool_ids and known_tool_ids[tc_id] not in matched_tool_groups
|
|
}
|
|
if result_variants and not candidate_groups:
|
|
_retire_dropped_row(filtered, msg, leading)
|
|
repairs += 1
|
|
continue
|
|
if candidate_groups:
|
|
matched_tool_groups.add(min(candidate_groups))
|
|
filtered.append(msg)
|
|
_retire_leading_drops(filtered, leading)
|
|
return filtered, repairs
|
|
|
|
|
|
def _prune_unanswered_tool_calls(messages: list[dict]) -> tuple[list[dict], int]:
|
|
"""Pass 2: prune tool_calls not answered in the IMMEDIATELY following tool run (a displaced
|
|
result masks the per-call stub pass and strict providers 400). Payload-empty turns are
|
|
dropped; codex interims exempt."""
|
|
repairs = 0
|
|
pruned: list[dict] = []
|
|
leading: list[dict] = []
|
|
for i, msg in enumerate(messages):
|
|
if not (
|
|
isinstance(msg, dict) and msg.get("role") == "assistant" and msg.get("tool_calls")
|
|
and not _is_codex_interim(msg)
|
|
):
|
|
pruned.append(msg)
|
|
continue
|
|
answered: set = set()
|
|
for follower in messages[i + 1:]:
|
|
if not (isinstance(follower, dict) and follower.get("role") == "tool"):
|
|
break
|
|
tid = (follower.get("tool_call_id") or "").strip()
|
|
if tid:
|
|
answered.update(tool_result_id_variants(tid))
|
|
kept_calls = [tc for tc in msg["tool_calls"] if tool_call_id_variants(tc) & answered]
|
|
if len(kept_calls) != len(msg["tool_calls"]):
|
|
repairs += 1
|
|
if not kept_calls and not _msg_has_payload({k: v for k, v in msg.items() if k != "tool_calls"}):
|
|
# Pruned calls were the only payload; drop the turn (empty assistant messages 400).
|
|
_retire_dropped_row(pruned, msg, leading)
|
|
continue
|
|
if kept_calls:
|
|
msg["tool_calls"] = kept_calls
|
|
else:
|
|
msg.pop("tool_calls", None)
|
|
# tool_calls is part of the persisted row; rewriting it on a stamped dict stales the
|
|
# marker, so pop it or the flush scan skips the dict and the DB keeps the old calls.
|
|
msg.pop(_DB_PERSISTED_MARKER, None)
|
|
pruned.append(msg)
|
|
_retire_leading_drops(pruned, leading)
|
|
return pruned, repairs
|
|
|
|
|
|
def _merge_consecutive_users(messages: list[dict]) -> tuple[list[dict], int]:
|
|
"""Pass 3: merge consecutive plain-text user messages (no user input lost)."""
|
|
def _plain_text(content: Any) -> bool:
|
|
# never rewrite the persisted row around undecodable content
|
|
return isinstance(content, str) and not content.startswith(_CONTENT_JSON_PREFIX)
|
|
|
|
repairs = 0
|
|
merged: list[dict] = []
|
|
for msg in messages:
|
|
prev = merged[-1] if merged and isinstance(merged[-1], dict) else None
|
|
if (
|
|
prev is not None and prev.get("role") == "user"
|
|
and isinstance(msg, dict) and msg.get("role") == "user"
|
|
# A summary carrier followed by a new user row is a deliberate durable shape after
|
|
# retry/rewind; never mutate the persisted carrier (sanitizers merge copies later).
|
|
and split_user_originated_turn(prev)[0] is None
|
|
# A /steer row that ended the previous run is already persisted; merging the next
|
|
# prompt into it would rewrite it in place and re-break replay parity.
|
|
and prev.get("display_kind") != STEER_DISPLAY_KIND
|
|
# Plain-text only, and never a passive (``observed``) row into an addressed turn: the merged
|
|
# row keeps observed=True, so replay would fold the addressed text into the group context.
|
|
and _plain_text(prev.get("content", "")) and _plain_text(msg.get("content", ""))
|
|
and bool(prev.get("observed")) == bool(msg.get("observed"))
|
|
):
|
|
prev_content, new_content = prev.get("content", ""), msg.get("content", "")
|
|
merged_content = (
|
|
(prev_content + "\n\n" + new_content) if prev_content and new_content else (prev_content or new_content)
|
|
)
|
|
had_api_sidecar = "api_content" in prev
|
|
# Read before the marker is popped below. An unpersisted turn folded in ends the claim:
|
|
# the dict no longer stands for durable rows only.
|
|
if (prev.get(_DB_PERSISTED_MARKER) or prev.get(MERGED_DURABLE_ROWS)) and msg.get(_DB_PERSISTED_MARKER):
|
|
prev[MERGED_DURABLE_ROWS] = int(prev.get(MERGED_DURABLE_ROWS) or 1) + 1
|
|
else:
|
|
prev.pop(MERGED_DURABLE_ROWS, None)
|
|
prev["content"] = merged_content
|
|
# The clean-text persist override must replace only the absorbed turn, never the
|
|
# unanswered text before it; kept across replay passes (an empty turn absorbs too).
|
|
if prev_content:
|
|
prev[MERGED_TURN_PREFIX] = prev_content
|
|
# Merged content invalidates the api_content sidecar; drop it so replay cannot use stale bytes.
|
|
drop_stale_api_content(prev)
|
|
# A display-marker row (e.g. a model-switch marker persisted as role=user on
|
|
# purpose) merging with a plain user row must not bury the plain row's
|
|
# addressable identity: keeping the marker's display_kind hides the merged pair
|
|
# from every user-turn index used for rewind/submit addressing (display rows are
|
|
# excluded), so the plain row's durable id becomes unresolvable and the client's
|
|
# next prompt.submit fails closed with the input silently dropped. Keep
|
|
# the pair addressable instead: drop the display classification and carry the
|
|
# plain row's id, retiring the marker's own id onto the absorbed list. display_kind
|
|
# never reaches providers (stripped from every outgoing copy), so the merged
|
|
# turn's wire payload is unchanged. Deliberate scope: when BOTH rows carry
|
|
# display_kind (two consecutive model-switch markers) the pair keeps the first
|
|
# marker's classification and id — no plain row is swallowed there, so no
|
|
# addressable turn is lost.
|
|
if prev.get("display_kind") and not msg.get("display_kind"):
|
|
marker_row_id = prev.get("_row_id")
|
|
prev.pop("display_kind", None)
|
|
if msg.get("_row_id") is not None:
|
|
if isinstance(marker_row_id, int) and not isinstance(marker_row_id, bool):
|
|
absorbed_ids = prev.setdefault("_absorbed_row_ids", [])
|
|
if marker_row_id not in absorbed_ids:
|
|
absorbed_ids.append(marker_row_id)
|
|
prev["_row_id"] = msg["_row_id"]
|
|
# display_kind is part of the persisted row; reclassifying stales it even
|
|
# when the merged bytes reproduce the persisted content (empty absorb).
|
|
prev.pop(_DB_PERSISTED_MARKER, None)
|
|
# Pop the persist marker only when the durable row actually changed: a merge that
|
|
# reproduces the persisted bytes (e.g. an empty incoming turn) keeps its stamp.
|
|
if merged_content != prev_content or had_api_sidecar:
|
|
prev.pop(_DB_PERSISTED_MARKER, None)
|
|
_remember_absorbed_row(prev, msg, folded=True)
|
|
repairs += 1
|
|
continue
|
|
merged.append(msg)
|
|
return merged, repairs
|
|
|
|
|
|
_SEQUENCE_REPAIR_PASSES = (
|
|
_merge_consecutive_assistants, _drop_stray_tool_results, _prune_unanswered_tool_calls,
|
|
_merge_consecutive_users,
|
|
)
|
|
|
|
def _normalize_sentinel_encoded_content(messages: list[dict]) -> None:
|
|
"""Decode any sentinel-encoded row content in place before the alternation passes run, so an
|
|
image-bearing turn is never merged as text. A multimodal turn can re-enter the working set
|
|
as its ``\\x00json:[…]`` string (e.g. after a proactive prune re-inserts history). A body
|
|
that no longer parses stays a sentinel string; ``_merge_consecutive_users`` refuses to weld it."""
|
|
for msg in messages:
|
|
if not isinstance(msg, dict):
|
|
continue
|
|
content = msg.get("content")
|
|
decoded = _decode_content(content)
|
|
if decoded is not content:
|
|
msg["content"] = decoded
|
|
# Keep any `_db_persisted` marker: re-encoding reproduces the stored scalar, and dropping
|
|
# it makes the append-only flush re-append this user turn as a duplicate.
|
|
|
|
|
|
def repair_message_sequence(agent, messages: list[dict]) -> int:
|
|
"""Collapse malformed role-alternation left in the live history; returns repair count.
|
|
Providers require strict alternation after the system message (violations: silent empty
|
|
responses or 400s); this is the pre-call belt for host-fed, resumed or replayed histories.
|
|
Passes in order: decode any sentinel-encoded multimodal rows (so an image is not merged as text);
|
|
merge consecutive assistant turns (BEFORE orphan detection so the merged tool_call-id union is
|
|
known); drop stray tool results; prune unanswered tool_calls; merge consecutive user turns. A user
|
|
turn directly after an assistant turn is valid and left alone.
|
|
"""
|
|
if not messages:
|
|
return 0
|
|
_normalize_sentinel_encoded_content(messages)
|
|
repairs = 0
|
|
current = messages
|
|
for repair_pass in _SEQUENCE_REPAIR_PASSES:
|
|
current, made = repair_pass(current)
|
|
repairs += made
|
|
if repairs > 0:
|
|
# Rewrite in place so persistence/return value/DB flush see the repaired sequence.
|
|
messages[:] = current
|
|
return repairs
|
|
|
|
|
|
def _content_has_payload(content: Any) -> bool:
|
|
if isinstance(content, str):
|
|
return bool(content.strip())
|
|
if not isinstance(content, list):
|
|
return content not in (None, "")
|
|
# Any typed block counts, as long as a text block is not itself blank.
|
|
return any(
|
|
(block.get("type") != "text" or (isinstance(block.get("text"), str) and block["text"].strip()))
|
|
if isinstance(block, dict) else bool(block)
|
|
for block in content
|
|
)
|
|
|
|
|
|
def _msg_has_payload(msg: dict[str, Any]) -> bool:
|
|
"""True if ``msg`` carries anything the API treats as non-empty content (text, multimodal
|
|
blocks, tool_calls, reasoning). Role-agnostic counterpart of ``AIAgent._is_thinking_only_assistant``.
|
|
Codex Responses item carriers persist with content:"" by design (text lives in codex_*_items
|
|
and is replayed); treating them as payload keeps the repair from rewriting a designed-empty turn."""
|
|
return _content_has_payload(msg.get("content")) or bool(
|
|
msg.get("tool_calls")
|
|
or (isinstance(msg.get("reasoning_content"), str) and msg["reasoning_content"].strip())
|
|
or msg.get("reasoning")
|
|
or msg.get("reasoning_details")
|
|
or msg.get("codex_message_items")
|
|
or msg.get("codex_reasoning_items")
|
|
)
|
|
|
|
|
|
# Skill-scaffolding markers. A /skill (or /bundle) turn is expanded into a
|
|
# model-facing message embedding the full skill body; memory providers storing
|
|
# the raw user turn would capture the body instead of what the user asked, so
|
|
# ``extract_user_instruction_from_skill_message`` recovers just the instruction.
|
|
# The markers MUST stay byte-identical to the builders (``_build_skill_message``,
|
|
# ``_scaffold_header``).
|
|
_SKILL_INVOCATION_PREFIX = "[IMPORTANT: The user has invoked the "
|
|
_SINGLE_SKILL_MARKER = "The full skill content is loaded below.]"
|
|
_SINGLE_SKILL_INSTRUCTION = "The user has provided the following instruction alongside the skill invocation: "
|
|
_RUNTIME_NOTE = "\n\n[Runtime note:"
|
|
_BUNDLE_MARKER = " skill bundle,"
|
|
_BUNDLE_USER_INSTRUCTION = "\nUser instruction: "
|
|
_BUNDLE_FIRST_SKILL_BLOCK = "\n\n[Loaded as part of the "
|
|
|
|
# The skill name sits in the first quoted span of the activation note, for both
|
|
# the single-skill and the bundle header ("work" / "/clean /work").
|
|
_SKILL_NAME_RE = re.compile(re.escape(_SKILL_INVOCATION_PREFIX) + r'"([^"]*)"')
|
|
|
|
|
|
# Gateway auto-load scaffold (gateway/run_turn.py ``_hmwa_auto_load_skills``): a channel-bound
|
|
# skill prepended to the user's text on a NEW session. Unlike the invocation scaffolds above it
|
|
# carries no instruction marker — the user's text simply follows the payload blocks — so the
|
|
# describer strips the header + body and renders the typed request (session previews, titles).
|
|
_AUTO_LOAD_PREFIX = '[IMPORTANT: The "'
|
|
_AUTO_LOAD_SUFFIX_RE = re.compile(r'" skill is auto-loaded\. Follow its instructions for this session\.\]')
|
|
# Closing sentence of the skill-directory footer note (see _SKILL_DIR_NOTE in this module):
|
|
# the last thing in every built payload, so it marks where an auto-load payload ends and the
|
|
# next payload or the user's typed text begins.
|
|
_SKILL_DIR_NOTE_END = "then run them with the terminal tool using the absolute path."
|
|
_AUTO_LOAD_HEADER_RE = re.compile(re.escape(_AUTO_LOAD_PREFIX) + r'([^"]*)' + _AUTO_LOAD_SUFFIX_RE.pattern)
|
|
|
|
|
|
# Marks where a preview query joined the head and tail of a long scaffolded
|
|
# message; ``describe_skill_invocation`` cuts there rather than show the body.
|
|
SKILL_EXCERPT_JOINT = "\x1e"
|
|
|
|
|
|
def extract_user_instruction_from_skill_message(content: Any) -> Optional[str]:
|
|
"""Recover the user's instruction from a slash-skill-expanded or gateway
|
|
auto-load turn: the string unchanged when it is NOT scaffolding, the
|
|
extracted instruction when the scaffolding carried one, or ``None`` for a
|
|
bare ``/skill`` invocation."""
|
|
if not isinstance(content, str):
|
|
return None
|
|
if _AUTO_LOAD_HEADER_RE.match(content):
|
|
# Gateway auto-load scaffolding wraps the user's typed text in the same
|
|
# model-facing turn as the loaded skill bodies; keep only the typed text.
|
|
# A complete header is required so typed text that merely starts like one
|
|
# stays intact; a bare load carries no instruction.
|
|
return _auto_load_typed_text(content) or None
|
|
if not content.startswith(_SKILL_INVOCATION_PREFIX):
|
|
return content
|
|
if _BUNDLE_MARKER in content:
|
|
# Bundles put the instruction before the loaded skills: FIRST marker is the user's.
|
|
return _cut_after(content, _BUNDLE_USER_INSTRUCTION, _BUNDLE_FIRST_SKILL_BLOCK, content.find)
|
|
if _SINGLE_SKILL_MARKER in content:
|
|
# The instruction follows the skill body (which may quote the marker): LAST marker is the user's.
|
|
return _cut_after(content, _SINGLE_SKILL_INSTRUCTION, _RUNTIME_NOTE, content.rfind)
|
|
return None
|
|
|
|
|
|
def _describe_auto_loaded_skill_turn(content: str) -> Optional[str]:
|
|
"""``[IMPORTANT: The "X" skill is auto-loaded. …]`` + payload(s) + typed text
|
|
-> the typed text.
|
|
|
|
``_hmwa_auto_load_skills`` builds each payload with ``_build_skill_message``
|
|
(activation header, body, then the skill-directory footer note) and appends the
|
|
user's text as the final block. The footer's closing sentence is the reliable
|
|
payload-end marker: the user's text is what follows the LAST footer. A header
|
|
quoted inside a body (a skill embedding the scaffold in an example) carries no
|
|
footer, so it cannot end the payload early. ``/<skill>`` renders when no user
|
|
text follows, matching the single-skill describer's bare-invocation shape."""
|
|
if not content.startswith(_AUTO_LOAD_PREFIX):
|
|
return None
|
|
name_match = re.match(re.escape(_AUTO_LOAD_PREFIX) + r'([^"]*)"', content)
|
|
name = name_match.group(1) if name_match else ""
|
|
tail = _auto_load_typed_text(content)
|
|
if not tail:
|
|
return f"/{name}" if name else None
|
|
return " ".join(tail.split())
|
|
|
|
|
|
_AUTO_LOAD_SCAFFOLDS: OrderedDict[str, None] = OrderedDict()
|
|
_AUTO_LOAD_SCAFFOLDS_MAX = 32
|
|
_AUTO_LOAD_SCAFFOLDS_LOCK = threading.Lock()
|
|
|
|
|
|
def register_auto_load_scaffold(scaffold: str) -> None:
|
|
"""Record the exact payload text the gateway prepended to a turn's typed text, so the
|
|
boundary is known rather than guessed from prose the user may also have typed or pasted."""
|
|
if not scaffold:
|
|
return
|
|
with _AUTO_LOAD_SCAFFOLDS_LOCK:
|
|
_AUTO_LOAD_SCAFFOLDS[scaffold] = None
|
|
_AUTO_LOAD_SCAFFOLDS.move_to_end(scaffold)
|
|
while len(_AUTO_LOAD_SCAFFOLDS) > _AUTO_LOAD_SCAFFOLDS_MAX:
|
|
_AUTO_LOAD_SCAFFOLDS.popitem(last=False)
|
|
|
|
|
|
def _registered_auto_load_tail(content: str) -> Optional[str]:
|
|
"""Text after the longest registered scaffold *content* starts with; None on a miss."""
|
|
with _AUTO_LOAD_SCAFFOLDS_LOCK:
|
|
best = max((s for s in _AUTO_LOAD_SCAFFOLDS if content.startswith(s)), key=len, default=None)
|
|
return None if best is None else content[len(best):]
|
|
|
|
|
|
def _auto_load_typed_text(content: str) -> str:
|
|
"""The user's text after the auto-load payloads of a turn, stripped (``""`` when none).
|
|
The boundary the gateway registered wins; a turn built in another process (an old session
|
|
row) falls back to the FINAL payload footer, which ends with this exact sentence
|
|
(_build_skill_message)."""
|
|
registered = _registered_auto_load_tail(content)
|
|
if registered is not None:
|
|
return registered.strip()
|
|
footer_end = content.rfind(_SKILL_DIR_NOTE_END)
|
|
if footer_end == -1:
|
|
return ""
|
|
tail = content[footer_end + len(_SKILL_DIR_NOTE_END):].lstrip()
|
|
while block_end := next((end for start, end in _TRAILING_BLOCK_ENDS if tail.startswith(start)), None):
|
|
cut = tail.find(block_end)
|
|
tail = tail[cut + len(block_end):].lstrip() if cut >= 0 else ""
|
|
return tail.strip()
|
|
|
|
|
|
def describe_skill_invocation(content: Any, separator: str = " — ") -> Optional[str]:
|
|
"""Render a slash-skill-expanded turn the way the user typed it:
|
|
``"/work — fix the title leak"``, ``"/work"`` for a bare invocation, or
|
|
``None`` when *content* is not scaffolding. ``separator=" "`` gives the
|
|
literal invocation as typed (chat transcripts).
|
|
|
|
A gateway auto-load scaffold (channel-bound skill on a new session) is also
|
|
scaffolding: the typed request follows the skill payload, so it renders as
|
|
that request — the header/body never reaches a preview or a title.
|
|
"""
|
|
if not isinstance(content, str):
|
|
return None
|
|
if content.startswith(_AUTO_LOAD_PREFIX):
|
|
return _describe_auto_loaded_skill_turn(content)
|
|
if not content.startswith(_SKILL_INVOCATION_PREFIX):
|
|
return None
|
|
match = _SKILL_NAME_RE.match(content)
|
|
name = (match.group(1) if match else "").strip()
|
|
# Bundle headers already carry their typed "/a /b" keys; a single skill is a bare name.
|
|
label = name if name.startswith("/") else f"/{name}"
|
|
instruction = extract_user_instruction_from_skill_message(content)
|
|
if instruction and instruction is not content:
|
|
# An excerpt (head + tail joined by SKILL_EXCERPT_JOINT) can put the
|
|
# joint inside the span — keep only the side the marker was found on.
|
|
instruction = " ".join(instruction.split(SKILL_EXCERPT_JOINT)[0].split())
|
|
if instruction:
|
|
return f"{label}{separator}{instruction}" if name else instruction
|
|
return label if name else None
|
|
|
|
|
|
def _cut_after(message: str, marker: str, stop_marker: str, find) -> Optional[str]:
|
|
"""Text between *marker* (located with ``find``) and *stop_marker*, stripped; None if absent/empty."""
|
|
marker_idx = find(marker)
|
|
if marker_idx < 0:
|
|
return None
|
|
return message[marker_idx + len(marker):].split(stop_marker, 1)[0].strip() or None
|
|
|
|
|
|
# Closing text of the optional blocks _build_skill_message appends after the directory note; the
|
|
# auto-load describer skips them so they are never read as the user's typed request.
|
|
_SUPPORTING_FILES_END = "/scripts/foo.js`)."
|
|
_TRAILING_BLOCK_ENDS = (("[Skill config (", "\n]"), ("[Skill setup note: ", "]\n\n"),
|
|
("[This skill has supporting files", _SUPPORTING_FILES_END))
|
|
|
|
|
|
STALE_TOOL_CALL_MARKER_RE = re.compile(r"^\[[A-Za-z_][A-Za-z0-9_.-]*\]$")
|
|
|
|
|
|
HISTORY_CHANGED = "session history changed before the rewind could be persisted"
|
|
|
|
|
|
def user_indices(messages: list[dict[str, Any]]) -> list[int]:
|
|
return [i for i, m in enumerate(messages) if user_originated_turn_view(m) is not None]
|
|
|
|
|
|
def comparison_content(message: dict[str, Any]) -> Any:
|
|
"""Project content the way the durable row stores it (flush projection, then the read-side sanitize) so a
|
|
warm row and its durable twin compare equal."""
|
|
return _loaded_view_content(message.get("role"), _durable_content(message.get("content")))
|
|
|
|
|
|
# Public spellings of the durable-row projections above.
|
|
durable_content = _durable_content
|
|
is_ephemeral_scaffolding = _is_ephemeral_scaffolding
|