← Back to Plugins
Tools

Plugin Context Engine

ani6439walc By ani6439walc 👁 2 views ▲ 0 votes

Unified Context Engine plugin for OpenClaw

GitHub

Install

npm install
pnpm

Configuration Example

{
  "plugins": {
    "slots": { "contextEngine": "context-engine" },
    "entries": {
      "context-engine": {
        "enabled": true,
        "hooks": { "allowConversationAccess": true },
        "config": {}
      },
      "hindsight-memory": { "config": { "turnContextMode": "auto" } },
      "skill-harness": { "config": { "routing": { "turnContextMode": "auto" } } }
    }
  }
}

README

# Unified Context Engine

An OpenClaw 2026.12.1 context engine for plugins that prepare context for a single user turn. It starts contributors together, then appends their results to the model’s working history in priority order. The canonical user message and its persisted `modelPromptProjection` stay free of the temporary context. OpenClaw continues to own compaction and memory flushes.

## Enable the engine

Build the extension with `pnpm install` and `pnpm run build`. Add its directory to your installation’s plugin load paths if needed, allow its ID if your installation uses an allowlist, and select it in your OpenClaw config. Merge these fields into your existing settings.

```json
{
  "plugins": {
    "slots": { "contextEngine": "context-engine" },
    "entries": {
      "context-engine": {
        "enabled": true,
        "hooks": { "allowConversationAccess": true },
        "config": {}
      },
      "hindsight-memory": { "config": { "turnContextMode": "auto" } },
      "skill-harness": { "config": { "routing": { "turnContextMode": "auto" } } }
    }
  }
}
```

The conversation-access grant enables the passive run capture and successful-attempt cleanup hooks. Each contributor needs a version that implements the v1 registry. Either contributor can be used independently. In `auto` mode their existing prompt hooks can handle turns that the engine did not attempt. An installation with no participating contributors passes history through after stripping old envelopes.

Validate the merged config with `openclaw config validate`. Inspect the selected implementation with `openclaw plugins info context-engine --runtime --json`. To roll back, select the `legacy` slot and restore the contributors’ previous mode (the default is `hook`).

## Turn lifecycle

1. `before_model_resolve` captures available run and session identity. A failed model attempt keeps the capture for failover. Settled runs, ended sessions and plugin disposal release it, with a five-minute expiry backstop.
2. `assemble()` reads the current prompt and prior history. It skips empty or internal prompts and suppresses registered contributors on memory flushes. A locked model can omit the capture. The engine then derives the agent from the session key and uses the factory workspace. Live activity events requiring a run ID are unavailable on those turns.
3. Plugins register with `globalThis[Symbol.for("openclaw.turnContextContributors.v1")]`. A deadline bounds each contribution, including contributors that ignore cancellation. A failed contributor leaves the remaining results usable. The shared attempt ledger allows prompt hooks to yield after a skip, error or timeout.
4. The engine appends a single `<openclaw_turn_context version="1">` user message to the returned history. A later assembly strips any existing v1 envelope, including a timestamp-prefixed one. Downstream retention and conversation parsers must also recognize the envelope because tool loops and end-of-turn hooks can see it.
5. OpenClaw requires transcript fencing and atomic turn advancement for durable sessions. The engine reads the host-supplied history prefix and records a SHA-256 digest of each accepted advancement key with a boolean value in the host’s retained `turn-advancement` namespace. One receipt per accepted turn survives restart so a retry can return `duplicate`. Receipt storage grows with the number of accepted turns. Prompt, history and contributor output are never written to that store.

## Engine settings

| Setting                | Default | Behavior                                                                                                                       |
| ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `enabled`              | `true`  | Set to false to return the original history untouched.                                                                         |
| `contributorTimeoutMs` | `23000` | Default contributor deadline, 500–60000 ms.                                                                                    |
| `totalTimeoutMs`       | `25000` | Assembly deadline, 500–60000 ms and at least the default contributor deadline.                                                 |
| `contributorTimeouts`  | `{}`    | Individual deadlines by contributor ID, 500–60000 ms, capped at the remaining assembly deadline.                               |
| `maxInjectedChars`     | `24000` | Complete envelope length, 1000–120000 UTF-16 code units. Truncation preserves code points and the closing tag.                 |
| `minHeadroomTokens`    | `512`   | Skip preparation below this estimated free history budget, 0–16000 tokens. The text estimate is one token per four characters. |
| `disabledContributors` | `[]`    | Contributor IDs to exclude from ordinary fan-out.                                                                              |
| `ephemeralCacheOptOut` | `false` | Set to true to add `cacheCheckpoint: false`. This can reduce intra-turn cache reuse on direct Anthropic tool loops.            |
| `debugPerfTiming`      | `false` | Log per-contributor outcomes and durations. Assemblies with a run ID also emit `plugin:context-engine` timing events.          |

The appended user message can cause OpenClaw’s optional decision-based tool prefilter to retain the full baseline toolset. The automated two-turn check uses a local OpenAI-compatible provider fixture. It does not measure cache performance or exercise Google or Anthropic transport behavior.

## Development

```sh
pnpm install
pnpm run typecheck
pnpm run build
pnpm test
pnpm run test:plugin-loader
pnpm exec prettier --check .
pnpm pack --dry-run
```

The loader check creates a private, temporary OpenClaw home, stages the built package, validates the selected slot, and runs two embedded CLI turns against a loopback fixture. It checks the actual model requests and reads the isolated agent SQLite transcript, including persisted prompt projections. It removes the fixture after the check.
tools

Comments

Sign in to leave a comment

Loading comments...