Skip to content

fix(bub): recall with the user's text, not Bub's channel frame - #2488

Merged
hidb4ai merged 6 commits into
oceanbase:masterfrom
Fengzdadi:fix/bub-recall-query-envelope
Nov 13, 2026
Merged

hidb4ai merged 6 commits into
oceanbase:masterfrom
Fengzdadi:fix/bub-recall-query-envelope

Conversation

@Fengzdadi

@Fengzdadi Fengzdadi commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Which issue or RFC does this PR close?

Closes #2487.

Rationale for this change

The Bub plugin builds its POST /v1/context/prepare query from the latest user message in the model request. Bub's builtin build_prompt hook frames every channel message as:

<context_str>
---Date: <local ISO time>---
<content>

context_str is the message context, written as key=value|.... It always includes channel and chat_id, plus channel-specific keys such as sender_id. So a plain bub run "How should we deploy the migrations?" sends this query:

sender_id=human|channel=$cli|chat_id=local
---Date: 2026-11-04T14:12:23-05:00---
How should we deploy the migrations?

Lexical admission (fts_query_requirements in src/powercontext/builtin/artifacts/search.py) requires a candidate to match at least a quarter of the query's distinct analyzer terms. The frame adds 14 terms that no memory contains. For the remembered decision "Deploy database migrations before rolling out the application.":

Query Query terms Required matches Matched Admitted
How should we deploy the migrations? 2 1 2 yes
the framed prompt above 16 4 2 no

Two cases are affected:

  • A Server without an embedding model, which is the default. prepare returns empty for every ordinary Bub prompt.
  • A Server with embeddings, when a prepare falls back to lexical matching after the query-embedding budget is spent.

With a working embedding model, vector recall still finds the memory, but the frame still costs every Bub recall its lexical evidence.

The recall text has to come from each inbound message, not just the first one in a turn. Bub can steer a follow-up into a running turn: the steering inbox is drained into the next model step through build_prompt, without load_state. Following bubbuild/bub#400, the plugin implements build_prompt itself at tryfirst priority. It delegates to the other implementations and records which inbound text each built prompt came from. Bub calls build_prompt for the turn's message and for every drained follow-up, so each model-facing user message maps back to what the user sent. No prompt parsing and no new Bub hook are needed.

What changes are included in this PR?

  • integrations/bub/src/powercontext_bub/plugin.py:
    • build_prompt (tryfirst) builds the prompt through the other build_prompt implementations, resolved as Bub resolves them: in precedence order, awaiting each until one returns a result. It falls back to the message content as Bub does and returns the same prompt Bub would have built. It then records, in the turn state, the built prompt's text → the message's text (Bub's ChannelMessage.text, added after 0.6.1 by feat(channels): expose what the sender wrote on Telegram messages bubbuild/bub#399), or its content when text is absent or None. The prompt's text is recorded both as sent with steering and as the turn's message reaches the model after Bub's legacy prompt form joins its text parts with newlines.
    • If another plugin delegates build_prompt the same way and calls back into this one, it returns None so that the delegation continues instead of recursing.
    • before_llm_call recalls with the recorded text for the latest user message. A prompt Bub did not build from an inbound message, such as a subagent's, is recalled as the model sees it. An empty inbound text skips prepare, including an explicitly empty text beside JSON content.
    • The model request is unchanged. The user_prompt capture event still records the model-facing prompt, and the 8,192-character/byte bound still applies to the query.
  • e2e/bub/tests/test_bub_recall_query.py runs Bub's own framework with its builtin hooks and the plugin registered after them, as Bub loads it. It checks:
    • The query is the message content for a CLI message, a chat-channel message with extra context keys and multi-line content, and a CLI message whose text is JSON.
    • The query is text when a message carries it beside JSON content.
    • An empty or whitespace-only text beside JSON content skips prepare, also through process_inbound; with text set to None or absent, the query is the JSON content.
    • An empty message skips prepare.
    • A prompt built by another plugin registered after this one still maps back, including one built from several text parts through process_inbound.
    • Another plugin delegating build_prompt the same way does not recurse.
    • A subagent recalls with its own prompt.
    • A follow-up steered into a running turn through process_inbound and Bub's steering inbox recalls with its own text.
    • The shared recall-bound fixture cases.

Are there any user-facing changes?

Yes. Without an embedding model, automatic recall now works for normal prompts on Bub's CLI and on channels whose content is the user's text, including follow-ups steered into a running turn. Before, it returned empty unless the prompt exceeded the recall bound. With embeddings, these recalls also get lexical matches.

Telegram recall changes once a Bub release includes bubbuild/bub#399, which sets text. On Bub 0.6.1 it still uses the JSON content. Nothing changes in the API, configuration, or docs.

How was this change tested?

  • uv run --project e2e/bub python -m pytest e2e/bub/tests/test_bub_recall_query.py: 23 passed. With the plugin from master, 12 fail; with this PR's previous load_state version, 11 fail, including the steering case, where the follow-up recalls with the first question.
  • make harness-check: passed.
  • make check: passed.
  • Real Bub 0.6.1 steering run: Bub's framework with its builtin hooks and the installed powercontext entry-point plugin, process_inbound("How should we deploy the migrations?"), and a follow-up steered into the turn after the first model call. It ran against a local lexical-only SQLite PowerContext Server holding a migration decision and a support escalation policy, with a recording OpenAI-compatible stub model.
    • build_prompt resolved as powercontext, then builtin.
    • With this change, the first model request carried the migration decision and the second the support policy.
    • With the previous load_state version, both carried the migration decision.
  • Real Bub 0.6.1 empty-text run: Bub's framework with its builtin hooks and the plugin registered after them, process_inbound of supplied Telegram-shaped envelopes, a local lexical-only SQLite PowerContext Server holding the support escalation policy, and a recording stub model.
    • With text="" and JSON content that quotes the policy as reply_to_message, no prepare was issued and no context was injected. Falling back with text or content instead prepared with the JSON and injected the policy.
    • text=" " issued no prepare. text set to the question, and JSON content without text, each prepared with that text or JSON and injected the policy.
  • Self-review against REVIEW.md: a follow-up steered into a running turn goes through the same build_prompt mapping, so an empty text there also skips recall. No other integration falls back from text to content.
  • Not run: a real Telegram bot, since Bub 0.6.1 does not set text yet.

AI usage statement

This PR was prepared with Claude Code (Claude Opus 5.5). It traced Bub's prompt framing, steering, and hook resolution, made the change, ran the checks and the real Bub runs, and drafted this description. I reviewed the change and the results.

Copilot AI balanced review requested due to automatic review settings November 5, 2026 04:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Fengzdadi
Fengzdadi force-pushed the fix/bub-recall-query-envelope branch from 2a22156 to 9481d4b Compare November 5, 2026 04:42
Copilot AI balanced review requested due to automatic review settings November 5, 2026 04:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@PsiACE PsiACE left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Could we target bub[app] 0.6.1 and obtain recall text through standard hooks instead of reverse-parsing model prompts?

The current unwrapping also turns CLI input {"message":"report","constraint":"database migrations"} into report, dropping the constraint and missing relevant memories. For Telegram, we should expose searchable text before JSON wrapping, with a small upstream change if needed, while preserving the model-facing messages.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional end-to-end evidence for @PsiACE's existing JSON-input finding, verified at 9481d4b4 with real Bub 0.5.1 CLI, a local SQLite PowerContext Server without embeddings, and a recording OpenAI-compatible model stub.

With a stored rollout-procedure memory, this CLI input recalls and injects it on base 4b50c158:

{"message":"Help","task":"Recall our canary rollout procedure: deploy database migrations before rolling out the application; notify the release team and verify health checks."}

At this head, the prepare request contains only Help, and the model receives no recalled context. Changing message to "" skips prepare entirely despite the nonempty task. The ordinary question How should we deploy the migrations? does start recalling correctly, confirming both the intended fix and the JSON regression.

Please preserve user-authored JSON fields when extracting recall text; a message key alone does not establish that the content is a channel envelope.

@Fengzdadi

Copy link
Copy Markdown
Contributor Author

Thanks for the fix. Could we target bub[app] 0.6.1 and obtain recall text through standard hooks instead of reverse-parsing model prompts?

The current unwrapping also turns CLI input {"message":"report","constraint":"database migrations"} into report, dropping the constraint and missing relevant memories. For Telegram, we should expose searchable text before JSON wrapping, with a small upstream change if needed, while preserving the model-facing messages.

Thanks for the reply; I’ll refactor it a bit later.

Bub's builtin build_prompt prefixes each channel message with its context and the time, which outweighs the user's words in lexical recall. The plugin now takes the recall text from the inbound message in load_state, before that framing, and keeps the model request unchanged.
@Fengzdadi
Fengzdadi force-pushed the fix/bub-recall-query-envelope branch from 9481d4b to ff44db7 Compare November 5, 2026 08:06
Copilot AI balanced review requested due to automatic review settings November 5, 2026 08:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Fengzdadi

Copy link
Copy Markdown
Contributor Author

Thanks, @PsiACE. Reworked along those lines:

  • Bub 0.6.1: feat(bub): target Bub 0.6.1 #2492 moves the integration to bub[app]>=0.6.1,<0.7.0. The plugin now reads and injects republic.Message, and the harness uses bub-acp-server 0.1.3. It changes no behavior, so it can be reviewed alone. This PR builds on it.
  • Recall text from a standard hook: the plugin no longer parses the model prompt. load_state keeps the inbound message's content, before build_prompt frames it, and before_llm_call recalls with that. Your CLI example {"message":"report","constraint":"database migrations"} is now recalled whole; it is a test case. A subagent runs under its own session with a copy of the turn state, so it recalls with its own prompt.
  • Telegram: feat(channels): expose what the sender wrote on Telegram messages bubbuild/bub#399 adds ChannelMessage.text, the sender's text before the JSON wrapping, joined by from_batch. The model-facing content stays the same. Telegram recall is unchanged here; once #399 is in a release, a follow-up will have the plugin read text.

Verified with real bub run on 0.6.1 against a lexical-only Server. This change injects the remembered decision. The plugin from #2492 alone does not.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous JSON-input regression is fixed. A same-session steering regression remains; reproduction is attached inline.

@knqiufan knqiufan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing same-session steering finding still applies: subsequent recall uses the earlier inbound text. Please address the existing thread before merging.

@hidb4ai hidb4ai removed the 1.5.1 v1.5.1 release tracking label Nov 9, 2026
@AlexStocks AlexStocks added the 1.5.2 v1.5.2 release tracking label Nov 9, 2026
Bub drains steered follow-ups into a running turn through build_prompt
without load_state, so the per-turn recall text went stale. The plugin
now implements build_prompt at tryfirst priority, delegates to the other
implementations in Bub's resolution order, and records the inbound text
(ChannelMessage.text when set, else content) for each built prompt.
before_llm_call recalls with the text of the latest user message.
The turn's message reaches the model through Bub's legacy prompt form,
which joins text parts with newlines, so record the recall text under
that form too. A plugin delegating build_prompt the same way would call
back into this one without end; return None when re-entered so the
delegation continues with the remaining implementations.
@Fengzdadi
Fengzdadi marked this pull request as ready for review November 11, 2026 20:40
Copilot AI balanced review requested due to automatic review settings November 11, 2026 20:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at 80eb5941: the previous JSON-input and same-session steering issues are fixed, including the real Bub steering path against a lexical-only SQLite Server. One empty-text fallback issue remains, detailed inline.

@hireview001 hireview001 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Bub sets `text` to None when `content` is what the sender wrote
(bubbuild/bub#399). Fall back to `content` only then, so an empty
`text` skips recall instead of recalling the JSON envelope with its
metadata and replied-to message.
Copilot AI balanced review requested due to automatic review settings November 12, 2026 23:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hidb4ai hidb4ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 3a28a015. The empty-text fallback is fixed: explicit empty/blank text skips prepare and context injection, while absent/None text preserves the full JSON content. Verified through real Bub 0.6.1 process_inbound and steering against a lexical-only SQLite Server with a recording model stub. No remaining blocking findings; no database tables or public APIs added. Live Telegram was not exercised.

@hidb4ai

hidb4ai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

The code review at 3a28a015 is approved. The PR currently has a merge conflict with current master (2d479277): a read-only git merge-tree reproduces a content conflict in e2e/bub/tests/test_bub_recall_query.py. Please resolve that test-file conflict before merging and rerun the affected checks.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 08:39
@hidb4ai
hidb4ai merged commit 0a934c6 into oceanbase:master Nov 13, 2026
2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The hook loop re-enters a modifying delegator, duplicating its prefix and leaving PowerContext mapped to the inner prompt, so the final prompt loses recall metadata and recall falls back to channel-framed text.

1 open issue

Review effort: Balanced

Comment on lines +207 to +210
others = self._framework.plugin_manager.subset_hook_caller("build_prompt", remove_plugins=[self])
kwargs = {"message": message, "session_id": session_id, "state": state}
for impl in reversed(others.get_hookimpls()):
result = impl.function(**{name: kwargs[name] for name in impl.argnames if name in kwargs})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.5.2 v1.5.2 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(bub): recall query includes Bub's channel frame, so lexical recall returns empty

8 participants