Skip to content

feat: prompt-aware context assembly with BM25-lite relevance scoring#180

Open
GodsBoy wants to merge 2 commits intoMartian-Engineering:mainfrom
GodsBoy:feat/prompt-aware-assembly
Open

feat: prompt-aware context assembly with BM25-lite relevance scoring#180
GodsBoy wants to merge 2 commits intoMartian-Engineering:mainfrom
GodsBoy:feat/prompt-aware-assembly

Conversation

@GodsBoy
Copy link
Contributor

@GodsBoy GodsBoy commented Mar 25, 2026

Summary

Refs OpenClaw PR #50848 (merged 21 Mar 2026). Adds lightweight relevance scoring during eviction so summaries matching the user's prompt are preferred over chronological order. Zero cost (no LLM calls), fully backwards compatible.

What changed

  • AssembleContextInput gains prompt?: string
  • LcmContextEngine.assemble() params gains prompt?: string and threads it to the assembler
  • ResolvedItem gains text: string (pre-extracted plain text for scoring, set during resolution)
  • New scoreRelevance() pure function: BM25-lite TF keyword overlap, no external deps
  • Eviction logic: when prompt is set and non-empty, items are scored and filled greedily (highest score first); output is re-sorted by ordinal to preserve chronological order. Empty/absent prompt → existing chronological behavior unchanged.

Tests

6 new integration tests in test/lcm-integration.test.ts:

  • Prompt-aware eviction prefers relevant summaries over irrelevant ones
  • Falls back to chronological when no prompt provided
  • Empty string prompt → chronological fallback (no crash)
  • Budget fits everything → prompt has no effect on output
  • Single evictable item: kept if it fits, dropped if not
  • Output messages remain in chronological order after prompt-aware selection

All 213 existing tests pass. The 11 pre-existing failures are unrelated (missing @sinclair/typebox / @mariozechner/pi-coding-agent packages).

Post-Deploy Monitoring & Validation

No additional operational monitoring required: this is a pure in-process algorithm change with no DB writes, no external calls, and no persistent state. The prompt parameter is optional — all existing callers are unaffected.

GodsBoy added 2 commits March 25, 2026 08:11
When the token budget is exceeded during context assembly, evictable items
are now scored by relevance to the current user prompt (BM25-lite TF keyword
scoring) rather than dropped in strict chronological order. This means
summaries matching the user's active query are preserved over irrelevant
but more recent content.

- Add `prompt?: string` to AssembleContextInput and LcmContextEngine.assemble()
- Add `text: string` to ResolvedItem for pre-extracted scoring content
- Implement scoreRelevance() using TF-based keyword overlap (no deps, no LLM)
- Fall back to existing chronological eviction when prompt is absent or empty
- Add 6 integration tests covering prompt-aware eviction, fallback, and edge cases

Refs OpenClaw PR #50848. Zero cost increase, fully backwards compatible.
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