Skip to content

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

Merged
jalehman merged 4 commits into
Martian-Engineering:mainfrom
GodsBoy:feat/prompt-aware-assembly
Apr 3, 2026
Merged

feat: prompt-aware context assembly with BM25-lite relevance scoring#180
jalehman merged 4 commits into
Martian-Engineering:mainfrom
GodsBoy:feat/prompt-aware-assembly

Conversation

@GodsBoy

@GodsBoy GodsBoy commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

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 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

13 unit tests in test/assembler-blocks.test.ts:

  • tokenizeText: splitting, lowercasing, single-char filtering, empty input, mixed punctuation/numbers
  • scoreRelevance: empty inputs, no overlap, positive overlap, multi-term scoring, prompt term deduplication, case insensitivity, single-char term filtering

All 226 tests pass. The 11 pre-existing test file 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

GodsBoy commented Mar 29, 2026

Copy link
Copy Markdown
Contributor Author

@jalehman — This PR is ready for review.

Added 13 unit tests for the BM25-lite scoring functions (scoreRelevance, tokenizeText) on top of the existing 6 integration tests. CI is green (32s). All 226 tests pass.

Quick summary of the approach: when AssembleContextInput.prompt is set, evictable items are scored by keyword overlap (BM25-lite TF) and budget is filled greedily from highest-scoring items. No LLM calls, fully backwards compatible — absent/empty prompt falls back to existing chronological eviction.

GodsBoy added 3 commits April 3, 2026 12:24
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.
@GodsBoy GodsBoy force-pushed the feat/prompt-aware-assembly branch from a046106 to 592b2b4 Compare April 3, 2026 10:26
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.
@jalehman

jalehman commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@jalehman jalehman merged commit ea84f45 into Martian-Engineering:main Apr 3, 2026
2 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 3, 2026
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.

2 participants