fix(agent): remove local context tail truncation (#5820)
* fix(agent): remove local context tail truncation * fix(agent): defer subagent type annotations * fix(agent): require context consolidator for runner * refactor(agent): minimize consolidator contract diff
This commit is contained in:
1 parent
8d0430c761
commit
29ba4c7f2d
18 files changed
+483
-855
No files matched your search
+27
-13
@@ -778,11 +778,11 @@ _ARCHIVE_TOOL_RESULT = (
|
||||
|
||||
|
||||
class MemoryArchiver:
|
||||
"""Write durable transcript batches to the Memory ingestion journal.
|
||||
"""Generate transcript checkpoints and optionally journal their source.
|
||||
|
||||
The archiver deliberately has no SessionManager dependency: it may read a
|
||||
captured transcript batch and append to history.jsonl, but it cannot mutate
|
||||
provider continuation state or advance a session watermark.
|
||||
captured transcript batch and, for durable sessions, append to history.jsonl,
|
||||
but it cannot mutate provider continuation state or advance a session watermark.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -804,9 +804,14 @@ class MemoryArchiver:
|
||||
session_key: str,
|
||||
previous_summary: str | None,
|
||||
max_tokens: int,
|
||||
persist: bool = True,
|
||||
) -> str:
|
||||
"""Persist the failed chunk and return a bounded replacement checkpoint."""
|
||||
raw = self.store.raw_archive(messages, session_key=session_key)
|
||||
"""Return a bounded raw checkpoint, optionally persisting its source."""
|
||||
raw = (
|
||||
self.store.raw_archive(messages, session_key=session_key)
|
||||
if persist
|
||||
else self.store._build_raw_checkpoint(messages)
|
||||
)
|
||||
return self._combine_raw_checkpoint(
|
||||
raw,
|
||||
previous_summary=previous_summary,
|
||||
@@ -857,8 +862,9 @@ class MemoryArchiver:
|
||||
input_token_budget: int | None = None,
|
||||
fallback_max_tokens: int | None = None,
|
||||
provider_state: ProviderConversationState | None = None,
|
||||
persist: bool = True,
|
||||
) -> str | None:
|
||||
"""Append the archive prompt to H and persist its summary."""
|
||||
"""Generate a replacement checkpoint and optionally persist it."""
|
||||
if not source_messages:
|
||||
return None
|
||||
|
||||
@@ -872,6 +878,7 @@ class MemoryArchiver:
|
||||
if fallback_max_tokens is not None
|
||||
else runtime.generation.max_tokens
|
||||
),
|
||||
persist=persist,
|
||||
)
|
||||
|
||||
prompt = render_template(
|
||||
@@ -920,7 +927,8 @@ class MemoryArchiver:
|
||||
)
|
||||
if input_token_budget <= 0 or estimated > input_token_budget:
|
||||
logger.debug(
|
||||
"Memory archive input does not fit for {}: {}/{} via {}; raw-dumping",
|
||||
"Memory archive input does not fit for {}: {}/{} via {}; "
|
||||
"using raw checkpoint",
|
||||
session_key,
|
||||
estimated,
|
||||
input_token_budget,
|
||||
@@ -944,7 +952,7 @@ class MemoryArchiver:
|
||||
except Exception:
|
||||
phase = "provider call" if attempt == 0 else "tool-call recovery"
|
||||
logger.warning(
|
||||
"Memory archive {} failed, raw-dumping to history",
|
||||
"Memory archive {} failed; using raw checkpoint",
|
||||
phase,
|
||||
)
|
||||
return raw_fallback()
|
||||
@@ -996,22 +1004,24 @@ class MemoryArchiver:
|
||||
assert response is not None
|
||||
if response.finish_reason in {"error", "length"}:
|
||||
logger.warning(
|
||||
"Memory archive provider did not complete ({}), raw-dumping to history",
|
||||
"Memory archive provider did not complete ({}); using raw checkpoint",
|
||||
response.finish_reason,
|
||||
)
|
||||
return raw_fallback()
|
||||
if response.has_tool_calls is True:
|
||||
logger.warning("Memory archive provider returned tool calls, raw-dumping to history")
|
||||
logger.warning("Memory archive provider returned tool calls; using raw checkpoint")
|
||||
return raw_fallback()
|
||||
summary = response.content
|
||||
if not summary or not summary.strip():
|
||||
logger.warning("Memory archive provider returned no summary, raw-dumping to history")
|
||||
logger.warning("Memory archive provider returned no summary; using raw checkpoint")
|
||||
return raw_fallback()
|
||||
summary = self.store._normalize_history_entry(summary)
|
||||
if not summary:
|
||||
logger.warning("Memory archive provider summary was not safe to replay, raw-dumping")
|
||||
logger.warning(
|
||||
"Memory archive provider summary was not safe to replay; using raw checkpoint"
|
||||
)
|
||||
return raw_fallback()
|
||||
if summary != "(nothing)":
|
||||
if persist and summary != "(nothing)":
|
||||
self.store.append_history(summary, session_key=session_key)
|
||||
return summary
|
||||
|
||||
@@ -1131,6 +1141,7 @@ class Consolidator:
|
||||
session_key: str,
|
||||
tools: list[dict[str, Any]],
|
||||
provider_state: ProviderConversationState | None = None,
|
||||
persist: bool = True,
|
||||
) -> str | None:
|
||||
"""Summarize the exact transcript prefix already accepted by the model."""
|
||||
source_messages = [
|
||||
@@ -1158,6 +1169,7 @@ class Consolidator:
|
||||
input_token_budget=input_token_budget,
|
||||
fallback_max_tokens=max(1, checkpoint_tokens),
|
||||
provider_state=provider_state,
|
||||
persist=persist,
|
||||
)
|
||||
if summary is None:
|
||||
return None
|
||||
@@ -1172,6 +1184,7 @@ class Consolidator:
|
||||
runtime: LLMRuntime,
|
||||
session_key: str,
|
||||
tools: list[dict[str, Any]],
|
||||
persist: bool = True,
|
||||
) -> str | None:
|
||||
"""Prompt a native compacted state without replaying its raw history."""
|
||||
return await self.summarize_transcript(
|
||||
@@ -1181,6 +1194,7 @@ class Consolidator:
|
||||
session_key=session_key,
|
||||
tools=tools,
|
||||
provider_state=state,
|
||||
persist=persist,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in new issue
Block a user