Skip to content

v0.10: Context Engine And Compression [PARTIAL]

Goal

Keep long-running character sessions useful without stuffing everything into every prompt.

Implementation Status

  • [x] Pluggable ContextEngine interface.
  • [x] DefaultContextEngine adapter.
  • [x] Deterministic prompt section building.
  • [x] ContextCompressor (Initial: summarization + tool pruning + fallback).
  • [x] Replace /compact internals with ContextCompressor reference digest.
  • [ ] Automated compression triggering in AgentLoop.
  • [ ] Dedicated advanced tests/token budgets/iterative structured compression.

Scope

  • Add pluggable context engine interface.
  • Build prompt sections in a stable order.
  • Add structured running summary.
  • Protect recent tail and identity/profile head.
  • Prune tool output.
  • Redact sensitive data.
  • Add fallback model behavior for compression failures.
  • Make summaries reference-only, not new instructions.
  • Add tests for prompt injection from context files, memory blocks, and retrieved transcripts.

Prompt Order

  1. platform/system policy
  2. character profile
  3. current channel/session state
  4. relevant memory
  5. relevant session search
  6. active skills/routines
  7. recent messages

Module Targets

  • backend/agent/g_agent/context/engine.py
  • backend/agent/g_agent/context/compressor.py
  • backend/agent/g_agent/context/redaction.py
  • backend/agent/g_agent/agent/context.py
  • backend/agent/tests/test_context_engine.py
  • backend/agent/tests/test_context_compression.py

Acceptance Criteria

  • Prompt assembly order is deterministic.
  • Memory and retrieved transcripts are fenced.
  • Recent turns are protected from over-compression.
  • Large tool output is summarized or pruned.
  • Compression failure degrades gracefully.

References

  • hermes-agent-ref/agent/context_engine.py
  • hermes-agent-ref/agent/context_compressor.py
  • hermes-agent-ref/agent/prompt_builder.py

Agent Handoff

Current G-Agent State

  • ContextBuilder is monolithic and builds system prompt + runtime context.
  • ContextBuilder.strip_runtime_context() already prevents stale runtime metadata from being persisted.
  • Session.get_history(max_messages=50) currently truncates history by message count, not tokens.
  • There is a /compact command that replaces current session messages with a digest built by SessionManager._build_digest().

Implementation Strategy

Do not replace ContextBuilder in one pass. Add a context engine interface and move behavior behind it gradually.

Recommended shape:

  • context/engine.py: interface.
  • context/default_engine.py: wraps current ContextBuilder.
  • context/compressor.py: future compression engine.
  • context/redaction.py: secret redaction helpers.

Implementation Slices

  1. Add deterministic prompt section builder.
  2. Preserve current prompt output as much as possible.
  3. Add token/char budgeting.
  4. Start char-based if token counter is unavailable.
  5. Add tool-output pruning for saved history.
  6. Summarize large tool results.
  7. Add structured compression.
  8. Protected head: identity/profile.
  9. Protected tail: latest turns.
  10. Summarize middle.
  11. Add automatic compression trigger in AgentLoop when stable.

Tests

  • test_context_engine.py
  • section order
  • memory fencing
  • profile/head protection
  • test_context_compression.py
  • large middle history summarized
  • recent tail preserved
  • tool output pruned
  • compression failure fallback
  • test_prompt_injection_context.py
  • malicious memory/context file does not become instructions.

Guardrails

  • Do not remove strip_runtime_context.
  • Do not summarize away unresolved tasks, paths, commands, URLs, or decisions.
  • Do not let summaries become instructions.

First PR Boundary

Context engine interface + current builder adapter + tests proving output order. Shipped with first-slice compression (summarize middle, fallback, prune tool outputs) but lacks dedicated advanced tests/token budgets/iterative structured compression.