Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions code_puppy/agents/_compaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def compact(
messages: List[ModelMessage],
model_max: int,
context_overhead: int,
force: bool = False,
) -> Tuple[List[ModelMessage], List[ModelMessage]]:
"""Unified compaction entrypoint. Replaces ``message_history_processor``.

Expand All @@ -290,6 +291,8 @@ def compact(
messages: Current message history (already accumulated by the caller).
model_max: Effective model context window in tokens.
context_overhead: Estimated overhead for system prompt + tool schemas.
force: If true, run the configured compaction strategy even below its
normal trigger. Used by the manual ``/compact`` command.

Returns:
``(new_messages, dropped_messages_for_hash_tracking)``.
Expand All @@ -312,12 +315,36 @@ def compact(
)
update_spinner_context(context_summary)

strategy = get_compaction_strategy()
if strategy == "continuity":
# This cannot currently live as a regular Code Puppy plugin without a
# new core extension point: compaction owns history-processor mutation
# and must preserve pydantic-ai tool-call/tool-return ordering.
from code_puppy.agents.continuity_compaction import compact_continuity

result_messages, summarized_messages = compact_continuity(
agent=agent,
messages=messages,
model_max=model_max,
context_overhead=context_overhead,
model_name=model_name,
force=force,
)
final_token_count = sum(
estimate_tokens_for_message(m, model_name) for m in result_messages
)
final_summary = SpinnerBase.format_context_info(
final_token_count,
model_max,
final_token_count / model_max if model_max else 0.0,
)
update_spinner_context(final_summary)
return result_messages, summarized_messages

threshold = get_compaction_threshold()
if proportion_used <= threshold:
if not force and proportion_used <= threshold:
return messages, []

strategy = get_compaction_strategy()

protected_tokens = get_protected_token_count()
filtered = filter_huge_messages(messages, model_name)

Expand Down
14 changes: 14 additions & 0 deletions code_puppy/agents/continuity_compaction/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Continuity-oriented message-history compaction.

Continuity is intentionally wired through the core compaction path instead of
the current plugin system. Code Puppy plugins can register commands, tools,
model types, prompts, and tool/run hooks, but they do not have a first-class
extension point for replacing the history processor's compaction decision or
mutating pydantic-ai message history while preserving tool-call/tool-return
ordering. Until such an extension point exists, keeping this strategy in the
core compaction path is safer than monkeypatching compaction from a plugin.
"""

from code_puppy.agents.continuity_compaction.engine import compact_continuity

__all__ = ["compact_continuity"]
Loading
Loading