Skip to content

feat: expose context-engine compaction delegate helper#49061

Merged
jalehman merged 4 commits into
mainfrom
codex/context-engine-compaction-delegate
Mar 18, 2026
Merged

feat: expose context-engine compaction delegate helper#49061
jalehman merged 4 commits into
mainfrom
codex/context-engine-compaction-delegate

Conversation

@jalehman

Copy link
Copy Markdown
Contributor

What

Adds a public delegateCompactionToRuntime(...) helper for context-engine plugins, refactors the built-in LegacyContextEngine to use the same bridge, and tightens the docs around the actual runtime meaning of ownsCompaction.

Why

The current context-engine docs make it too easy to read ownsCompaction: false as automatic fallback to legacy compaction, but the active engine still handles /compact and overflow recovery. That confusion came up in these review threads: #46409 (comment) and #46409 (comment). It also blocks the non-owning/delegating plugin path discussed in #43920, because third-party engines had no supported way to reuse OpenClaw's built-in compaction runtime.

Changes

  • Extract runtime compaction delegation into delegateCompactionToRuntime(...)
  • Export the helper from the plugin SDK
  • Refactor LegacyContextEngine to share the helper
  • Clarify owning vs delegating ownsCompaction semantics in docs
  • Document that non-owning engines should delegate from compact()
  • Add focused helper and plugin-sdk export coverage

Testing

  • pnpm build
  • pnpm test -- src/context-engine/context-engine.test.ts -t "delegateCompactionToRuntime|legacy compact preserves runtimeContext"
  • pnpm test -- src/plugin-sdk/index.test.ts -t "keeps the root runtime surface intentionally small|does not expose runtime modules"

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: S maintainer Maintainer-authored PR labels Mar 17, 2026
@jalehman
jalehman marked this pull request as ready for review March 17, 2026 14:32
@jalehman jalehman self-assigned this Mar 17, 2026
@greptile-apps

greptile-apps Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extracts the compaction delegation logic from LegacyContextEngine.compact() into a standalone delegateCompactionToRuntime(...) helper, exports it from openclaw/plugin-sdk, refactors the legacy engine to call it, and tightens documentation around ownsCompaction semantics.

Key changes:

  • src/context-engine/delegate.ts — new file containing the extracted bridge; logic is an exact copy of the removed LegacyContextEngine.compact() body, so behavior is unchanged
  • src/context-engine/legacy.ts — 40-line inline impl replaced with a single delegateCompactionToRuntime(params) call
  • src/plugin-sdk/core.ts and src/plugin-sdk/index.ts — export the new helper on both SDK entrypoints
  • Docs (compaction.md, context-engine.md, context.md, plugin.md) — clarify that ownsCompaction: false is not an automatic fallback and that non-owning engines must still implement compact() by delegating explicitly
  • Tests — new focused test verifies the helper and the existing legacy test still passes

One minor point to consider: params.compactionTarget ("budget" | "threshold") is received by delegateCompactionToRuntime as part of the ContextEngine.compact() signature but is silently not forwarded to compactEmbeddedPiSessionDirect (which has no such field in CompactEmbeddedPiSessionParams). This is correct and preserves pre-existing LegacyContextEngine behavior, but since delegateCompactionToRuntime is now a public API, a brief JSDoc note would prevent confusion for third-party plugin authors who pass compactionTarget and expect it to be honored.

Confidence Score: 5/5

  • Safe to merge — the refactoring is a faithful extraction with no behavior changes, the tests pass, and the documentation improvements are accurate.
  • The entire delegate.ts implementation is a verbatim copy of the removed LegacyContextEngine.compact() body, so there is zero risk of behavioral regression. Export paths are correctly wired in both SDK entrypoints, tests cover the new helper directly, and the documentation changes close a real semantic gap around ownsCompaction. The only note is a missing JSDoc clarification about compactionTarget not being forwarded, which is a documentation suggestion rather than a correctness concern.
  • No files require special attention.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/context-engine/delegate.ts
Line: 31-40

Comment:
**`compactionTarget` silently dropped without documentation**

`params.compactionTarget` is part of the `ContextEngine.compact()` interface (as `"budget" | "threshold"`), but it is not forwarded to `compactEmbeddedPiSessionDirect` — which doesn't have that field in `CompactEmbeddedPiSessionParams`. This is intentional and consistent with the original `LegacyContextEngine` behavior.

However, since `delegateCompactionToRuntime` is now a **public API** for third-party plugin authors, the silent discard of `compactionTarget` is not obvious. A caller who passes `compactionTarget` via their `compact()` params would reasonably expect it to be honored.

Consider adding a note to the JSDoc to make this explicit:

```ts
/**
 * ...
 *
 * Note: `compactionTarget` from the compact params is not forwarded to the
 * runtime bridge — the underlying runtime does not expose this knob via this
 * path. If you need compaction-target control, implement your own `compact()`
 * algorithm.
 */
```

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: 18f3de8

Comment thread src/context-engine/delegate.ts
@jalehman
jalehman force-pushed the codex/context-engine-compaction-delegate branch 2 times, most recently from bc06ff7 to 3a64241 Compare March 17, 2026 16:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a64241904

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread docs/zh-CN/tools/plugin.md Outdated
@jalehman
jalehman force-pushed the codex/context-engine-compaction-delegate branch from 3a64241 to cdef7a3 Compare March 17, 2026 19:39

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 205378e421

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/plugin-sdk/index.ts
@jalehman
jalehman force-pushed the codex/context-engine-compaction-delegate branch 4 times, most recently from 14f8281 to 86bce4f Compare March 18, 2026 02:57

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86bce4fe78

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread docs/tools/plugin.md Outdated
@JaySon-Huang

Copy link
Copy Markdown

Hi @jalehman ,

Thanks for taking the time to clarify the semantics of ownsCompaction: false and for providing an official compaction delegation path.

I think this is a very meaningful improvement to the contextEngine plugin model. It removes a lot of ambiguity for plugin authors and makes the non-owning / delegating mode much more practical for plugins that want to focus on long-term memory, retrieval augmentation, or other context-management behavior without having to implement their own compaction algorithm.

I believe this will help the OpenClaw context-engine ecosystem become both more mature and more vibrant over time.

@jalehman
jalehman force-pushed the codex/context-engine-compaction-delegate branch from 86bce4f to 2a9653c Compare March 18, 2026 05:25
@jalehman
jalehman merged commit 7f0f8dd into main Mar 18, 2026
26 of 47 checks passed
@jalehman
jalehman deleted the codex/context-engine-compaction-delegate branch March 18, 2026 05:54
fuller-stack-dev pushed a commit to fuller-stack-dev/openclaw that referenced this pull request Mar 20, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
fuller-stack-dev pushed a commit to fuller-stack-dev/openclaw that referenced this pull request Mar 20, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
ralyodio pushed a commit to ralyodio/openclaw that referenced this pull request Apr 3, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
* ContextEngine: add runtime compaction delegate helper

* plugin-sdk: expose compaction delegate through compat

* docs: clarify delegated plugin compaction

* docs: use scoped compaction delegate import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants