Skip to content

feat(bub): let the gateway own prompt-time recall - #2836

Merged
hidb4ai merged 1 commit into
oceanbase:masterfrom
Fengzdadi:feat/bub-prompt-recall
Nov 27, 2026
Merged

hidb4ai merged 1 commit into
oceanbase:masterfrom
Fengzdadi:feat/bub-prompt-recall

Conversation

@Fengzdadi

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

Part of #2250. This is step 5 for Bub, alongside #2754 (OpenCode), #2755 (Pi), and #2829 (Hermes).

Rationale for this change

Behind the PowerContext gateway, Core rejects every request that carries a prepared-context block the gateway did not inject, in every privacy mode. Core answers HTTP 422, and the gateway returns HTTP 503 gate_paused.

The Bub plugin produces such blocks in two ways:

  • Before each model call. before_llm_call prepends a system message with the prepared context. Bub does not write this message to the session's tape, so it affects only the model call it was added to.
  • The powercontext.context tool. Bub keeps a tool result on the session's tape and replays it with every later model call. Bub renders the tool's string result as-is, so the tool message carries the prepared text itself. fix(gateway): strip replayed plugin context before model admission #2809 strips only a JSON tool output shaped {"ok": true, "data": {"schema": "powercontext.prepared-context.v1", ...}} and a Responses developer message whose content is exactly the envelope. Neither matches a Bub tool message, so once the tool runs, Core rejects every later gateway request of that session.

Bub therefore needs a switch that hands prompt-time context to the gateway and withholds the tool.

What changes are included in this PR?

Setting: prompt_recall (POWERCONTEXT_BUB_PROMPT_RECALL, or prompt_recall in the powercontext section of Bub's configuration file), default true. The default keeps today's behavior.

With prompt_recall: false:

  • the plugin sends no preparation request and adds no context before a model call;
  • it does not offer powercontext.context. When a model calls it anyway, Bub's builtin unknown-tool handling answers that the tool does not exist;
  • powercontext.search, powercontext.remember, event capture, and flushes are unchanged;
  • with capture_events also off, the hook sends no request at all, not even the Scope resolution it would only have needed for recall.

Tool registration: the plugin now provides its tools through Bub's provide_tools hook, which can leave a tool out. Before, the tools were collected from @tool while Bub loaded the entry point. Bub 0.6.1 deprecates that path: it warns relies on @tool registering its tools, and it cannot exclude a tool. Loading through the entry point offers the same three tools by default, without that warning.

Docs: a new "Leave prompt-time context to the gateway" section in integrations/bub/README.md, modeled on the Hermes guide in #2829. Bub has no docs page. The section covers:

  • the switch, with event capture left off because the gateway captures turns. With capture off the plugin sends no flushes, so the gateway's captured turns become Memory through the Server's scheduled extraction, which needs both a generation model and POWERCONTEXT_SERVER_RUNTIME_SCHEDULE_SECONDS (neither set by default);
  • when a new session is needed. Only a session that already called powercontext.context needs one, because the per-call context is never on the tape. A new --session-id or Bub's tape.reset both work;
  • pointing Bub at /gateway/v1. The openai provider uses the Responses route, and Bub streams every call. On that route the gateway calls the model without streaming and replays the checked response. "api_format": "chat" or the openai-compatible provider would use streamed Chat Completions, which an enforcing Core withholds (HTTP 400). A non-enforcing Core releases the stream, and with PC_GATEWAY_SEDIMENT=on the gateway also captures it;
  • the Core target needed in every mode (docs(opencode): name the Core target the gateway needs #2796), and what an enforcing Core needs from Bub. By default Bub's Responses request carries no store, so an enforcing Core withholds every request with provider_state_unsupported and the gateway returns HTTP 400. BUB_COMPLETION_ARGS='{"extra_body": {"store": false}}' adds "store": false; Bub passes completion arguments to every model call, and republic merges extra_body into the body rather than sending it as a field. Bub always sends max_output_tokens (BUB_MAX_TOKENS, 16384 by default) and never prompt_cache_key, so it has no cache-retention requirement;
  • one shared Scope: POWERCONTEXT_BUB_SCOPE_ID and PC_GATEWAY_DEFAULT_SCOPE set to the same Scope, so the tools use the Scope the gateway fills;
  • a combined deployment, which injects Bub's requests by default since fix(gateway): replay context for headerless conversations #2813. Bub 0.6.1 sends no per-session header, and BUB_CLIENT_ARGS headers are the same for every session, so the gateway infers the conversation from the request. Bub's input qualifies: it sends no previous_response_id or conversation, and it opens with the system prompt as a system message followed by the first user message of the tape since the last anchor, both unchanged across the turn's tool-loop steps. The limits are stated: the inferred ID is local to the gateway process and serves only the replay; sessions that open with the same system prompt and first message share one; without a header, capture has no conversation identity that survives a gateway restart and prefix extraction is unavailable; after tape.handoff or tape.reset the gateway infers a new conversation. A standalone gateway still needs PC_GATEWAY_INJECTION=on, and a fixed X-PowerContext-Conversation-ID in BUB_CLIENT_ARGS would make every session one conversation.

The intro and the settings table link to this section.

Are there any user-facing changes?

Yes, one new opt-out setting, POWERCONTEXT_BUB_PROMPT_RECALL=false. The default is unchanged. There are no API or persisted-format changes.

How was this change tested?

New e2e/bub/tests/test_bub_prompt_recall.py drives one Bub turn through BubFramework.process_inbound, using Bub's builtin hooks, a stub Chat Completions model that can call tools, and a recording PowerContext client:

  • with recall off and capture on: no prepare request; no context in any model request; user_prompt and llm_result captured; one flush;
  • with recall off, the model calls powercontext_context and powercontext_search: the first is not offered and is answered as an unknown tool; no prepare request is sent; the search still runs;
  • with the default: the tool is offered and dispatched, and context is injected.

tests/e2e/test_bub_service_chain.py gains a case that runs bub run with POWERCONTEXT_BUB_PROMPT_RECALL=false and capture on, against a real in-process Server. It checks:

  • no /v1/context/prepare request, and no plugin context or powercontext_context tool in any model request;
  • powercontext_search still reaches the Server;
  • the prompt is still captured, and the turn flushes.

Against the plugin source on 4f829cb (#2820), the 3 new harness tests and the new service-chain case fail; the other 5 service-chain cases pass.

Commands, offline, on 1516580, based on master 4f829cb. Bub tests used an existing harness venv with bub 0.6.1 and republic 0.6.1, matching e2e/bub/uv.lock. It was pointed at this checkout with PYTHONPATH, and module paths were verified first.

  • python -m pytest e2e/bub/tests: 417 passed.
  • POWERCONTEXT_TEST_BUB_HOST=1 pytest tests/e2e/test_bub_service_chain.py (the bub-host job's command): 6 passed.
  • A one-off Bub 0.6.1 turn with the openai provider against a Responses stub: the request goes to /responses with model, input, tools, max_output_tokens: 16384, and stream: true and no store; with BUB_COMPLETION_ARGS='{"extra_body": {"store": false}}' it also carries "store": false, and no extra_body key.
  • The same turn against a Responses stub, checked against the gateway's conversation inference from fix(gateway): replay context for headerless conversations #2813: the input opens with one system item and a non-empty user message, carries no previous_response_id, conversation, or instructions, and its opening items are byte-identical across the model calls of one turn and across later turns of the session.
  • Entry-point loading (BubFramework.load_hooks(), with DeprecationWarning as an error): powercontext.context, powercontext.remember, and powercontext.search by default, and only the last two with POWERCONTEXT_BUB_PROMPT_RECALL=false.
  • pytest tests/test_automatic_prompt_privacy.py tests/test_integration_manifest.py: 672 passed.
  • ruff check and ruff format --check on e2e/bub and integrations/bub: passed.
  • ty check --project e2e/bub --python-version 3.12 --extra-search-path src e2e/bub/src integrations/bub/src, and the project's ty check: passed.
  • generate_integration_lifecycle.py --check and generate_integration_manifest_docs.py --check: passed.
  • prek run --files <changed files>: every hook passed except ty-check, which was skipped because it needs a synced environment. ty was run directly instead.

Not run:

  • Bub through a real gateway, and a real model. No existing test runs Bub behind the gateway, and the gateway at fix(gateway): replay context for headerless conversations #2813 needs crates that are not cached here, so it was not built. The README's gateway behavior comes from the gateway docs and source, and Bub's request shape from Bub's and republic's installed source and the stub turns above.
  • The Docker harness.
  • make docs-test. The README is not part of the website.

AI usage statement

This PR was written with Claude Code (Claude Opus 5.5), which read the code and Bub's installed source, implemented the change, wrote the tests and docs, and ran the checks above. A second Claude Code session reviewed the change against the code and the Bub 0.6.1 source, and reran the e2e/bub tests and the check against the previous plugin source.

Add a prompt_recall setting (POWERCONTEXT_BUB_PROMPT_RECALL, default
true). With false, the plugin sends no preparation request and adds no
context before a model call, and it does not offer powercontext.context.
Bub keeps a tool result on the session's tape and replays it with every
later model call. The gateway does not strip the plugin's plain prepared
text from a tool result, so Core would reject every later gateway request
of that session. Capture, the other tools and flushes are unchanged; with
capture off as well, the hook sends no request at all.

The plugin now provides its tools through Bub's provide_tools hook
instead of relying on @tool collection, which Bub 0.6.1 deprecates and
which cannot leave a tool out.

Document the gateway setup for Bub 0.6.1 in the plugin README: the
Responses route of the openai provider and how the gateway treats
streamed Chat Completions, the Core target and the store: false that an
enforcing Core requires (sent through BUB_COMPLETION_ARGS extra_body),
one shared Scope for the plugin and the gateway, scheduled extraction
for the turns the gateway captures, the conversation a combined
deployment infers from Bub's opening system and user messages, since
Bub sends no per-session header (a standalone gateway still needs
PC_GATEWAY_INJECTION=on), and that only a session that called the
prepare tool needs a fresh tape.

Part of oceanbase#2250.

@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

@hidb4ai
hidb4ai merged commit 869569e into oceanbase:master Nov 27, 2026
52 checks passed
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.

3 participants