(
messages: list[dict[str, Any]],
artifact_store: ArtifactStore,
semantic_compressor: SemanticCompressor,
policy: CompositionPolicy,
*,
session_id: str,
request: Any,
step_type: str,
is_agentic: bool,
)
| 470 | |
| 471 | |
| 472 | async def _checkpoint_history( |
| 473 | messages: list[dict[str, Any]], |
| 474 | artifact_store: ArtifactStore, |
| 475 | semantic_compressor: SemanticCompressor, |
| 476 | policy: CompositionPolicy, |
| 477 | *, |
| 478 | session_id: str, |
| 479 | request: Any, |
| 480 | step_type: str, |
| 481 | is_agentic: bool, |
| 482 | ) -> tuple[list[dict[str, Any]], str, str, SemanticCallResult | None] | None: |
| 483 | if step_type == "tool-selection" and policy.checkpoint_skip_tool_selection: |
| 484 | return None |
| 485 | |
| 486 | keep_last_messages = ( |
| 487 | policy.checkpoint_agentic_keep_last_messages |
| 488 | if is_agentic |
| 489 | else policy.checkpoint_keep_last_messages |
| 490 | ) |
| 491 | threshold_tokens = ( |
| 492 | policy.checkpoint_agentic_threshold_tokens |
| 493 | if is_agentic |
| 494 | else policy.checkpoint_threshold_tokens |
| 495 | ) |
| 496 | |
| 497 | if len(messages) < max(policy.checkpoint_min_messages, keep_last_messages + 2): |
| 498 | return None |
| 499 | if _estimate_messages_tokens(messages) < threshold_tokens: |
| 500 | return None |
| 501 | |
| 502 | leading_system, remainder = _split_leading_system(messages) |
| 503 | if len(remainder) <= keep_last_messages: |
| 504 | return None |
| 505 | if ( |
| 506 | is_agentic |
| 507 | and policy.checkpoint_skip_recent_tool_window > 0 |
| 508 | and _has_recent_tool_activity(remainder[-policy.checkpoint_skip_recent_tool_window:]) |
| 509 | ): |
| 510 | return None |
| 511 | |
| 512 | head = remainder[:-keep_last_messages] |
| 513 | tail = remainder[-keep_last_messages:] |
| 514 | transcript = _messages_to_transcript(head) |
| 515 | if not transcript.strip(): |
| 516 | return None |
| 517 | |
| 518 | history_record = artifact_store.store_text( |
| 519 | transcript, |
| 520 | kind="history-block", |
| 521 | role="system", |
| 522 | session_id=session_id, |
| 523 | content_type="text/plain", |
| 524 | ) |
| 525 | summary = history_record.summary |
| 526 | call: SemanticCallResult | None = None |
| 527 | if not summary: |
| 528 | call = await semantic_compressor.summarize_history( |
| 529 | transcript, |
no test coverage detected