feat: prompt-aware context assembly with BM25-lite relevance scoring#180
Merged
jalehman merged 4 commits intoApr 3, 2026
Merged
Conversation
Contributor
Author
|
@jalehman — This PR is ready for review. Added 13 unit tests for the BM25-lite scoring functions ( Quick summary of the approach: when |
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.
Export scoreRelevance and tokenizeText (with @internal JSDoc) for direct unit testing. Add 13 new tests covering edge cases: empty inputs, no overlap, case insensitivity, prompt term deduplication, single-char filtering, and relative scoring. Fix inaccurate docstring that claimed [0,1] bounded range.
a046106 to
592b2b4
Compare
Treat prompt-aware assembly as opt-in only when the prompt contains at least one searchable term. Blank or whitespace-only prompts now follow the existing chronological eviction path, and the integration suite covers that regression. Add a patch changeset because this fixes user-visible assembly behavior in the plugin. Regeneration-Prompt: | Review found that prompt-aware context eviction switched behavior on any non-empty prompt string, even when the string had no searchable terms after tokenization. Preserve the new relevance feature, but make blank, whitespace-only, or otherwise unsearchable prompts fall back to the existing chronological eviction path so behavior matches the docs and tests. Keep the change minimal in the assembler, add an integration test that proves whitespace-only prompts keep the chronological result, update public comments to reflect the actual contract, and add a patch changeset because this affects user-visible context assembly behavior.
Contributor
|
Thank you! |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
AssembleContextInputgainsprompt?: stringLcmContextEngine.assemble()params gainsprompt?: stringand threads it to the assemblerResolvedItemgainstext: string(pre-extracted plain text for scoring, set during resolution)scoreRelevance()pure function: BM25-lite TF keyword overlap, no external depspromptis 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 integration tests in
test/lcm-integration.test.ts:13 unit tests in
test/assembler-blocks.test.ts:tokenizeText: splitting, lowercasing, single-char filtering, empty input, mixed punctuation/numbersscoreRelevance: empty inputs, no overlap, positive overlap, multi-term scoring, prompt term deduplication, case insensitivity, single-char term filteringAll 226 tests pass. The 11 pre-existing test file failures are unrelated (missing
@sinclair/typebox/@mariozechner/pi-coding-agentpackages).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
promptparameter is optional — all existing callers are unaffected.