Skip to content

Comments

feat: add auto-compaction retry hook for embedded consumers#1318

Closed
1kuna wants to merge 3 commits intobadlogic:mainfrom
1kuna:feat/auto-compaction-retry-hook
Closed

feat: add auto-compaction retry hook for embedded consumers#1318
1kuna wants to merge 3 commits intobadlogic:mainfrom
1kuna:feat/auto-compaction-retry-hook

Conversation

@1kuna
Copy link

@1kuna 1kuna commented Feb 6, 2026

Summary

Auto-compaction can recover from an overflow, but the automatic retry can still overflow if the session’s system prompt (or embedded consumers’ injected context) remains too large. This can create an overflow → compaction → retry → overflow cascade.

This PR adds a small, embedded-consumer-focused hook that runs after auto-compaction has been applied but before Pi schedules the overflow retry, so consumers can:

  • keep the retry as-is
  • downgrade the system prompt for the retry (e.g. drop injected workspace context)
  • cancel the retry with a clear error message

Refs #1317.

What changed

  • AgentSession API
    • Added setAutoCompactionRetryHook(hook).
    • New types: AutoCompactionRetryHookContext, AutoCompactionRetryHookResult, AutoCompactionRetryHook.
    • The context includes post-compaction messages, systemPrompt, model + context window sizing, and token estimates (message/system/total/budget/overBy + overhead estimate).
    • Added a configurable timeout: compaction.autoCompactionRetryHookTimeoutMs (default: 1000ms).
  • Events
    • auto_compaction_end now includes retryCanceledMessage when compaction succeeds but the overflow retry is cancelled by the hook.
  • Tests
    • Added coverage for proceed / system-prompt override / cancel behavior.

Why this is useful

Embedded consumers may inject extra context (e.g. workspace files) into the system prompt. After compaction, the conversation may fit but the system prompt may still exceed the retry budget. The new hook provides a safe escape hatch to prevent runaway overflow cascades.

OpenClaw will use this hook to apply a slimmer retry prompt when needed.

Backward compatibility

  • No behavior change unless an embedded consumer registers the hook.
  • Hook failures/timeouts are treated defensively (they do not block retry by default).

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

Hi @1kuna, thanks for your interest in contributing!

We ask new contributors to open an issue first before submitting a PR. This helps us discuss the approach and avoid wasted effort.

Next steps:

  1. Open an issue describing what you want to change and why (keep it concise, write in your human voice, AI slop will be closed)
  2. Once a maintainer approves with lgtm, you'll be added to the approved contributors list
  3. Then you can submit your PR

This PR will be closed automatically. See https://github.com/badlogic/pi-mono/blob/main/CONTRIBUTING.md for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant