Skip to content

feat(gateway): bind model requests to an explicit Scope - #2810

Merged
hidb4ai merged 7 commits into
oceanbase:masterfrom
xixi-box:fix/issue-2804-5eb3f92-1592c872
Nov 27, 2026
Merged

hidb4ai merged 7 commits into
oceanbase:masterfrom
xixi-box:fix/issue-2804-5eb3f92-1592c872

Conversation

@xixi-box

Copy link
Copy Markdown
Contributor

Fixes #2804

Which issue or RFC does this PR close?

Closes #2804.

Rationale for this change

The shared model ingress always assigned PC_GATEWAY_DEFAULT_SCOPE to the managed call. On a single Gateway serving several projects, recall and captured turns therefore went to the same Scope even when the requests came from different projects.

What changes are included in this PR?

Read X-PowerContext-Scope-ID on Chat and Responses and carry the selected Scope through recall, replay, both Core checks, and capture. The Server sends the Source window's Scope on prefix extraction callbacks so the Gateway can look up the captured prefix within that Scope. Add process-level regression coverage, verify the per-candidate and one-pass callback headers, and update the English and Chinese guides.

Are there any user-facing changes?

Clients or trusted proxies can select a Scope with one header on each request. Requests without the header use the configured default. Invalid or repeated Scope headers return HTTP 400. Core applies its existing access rules under the Gateway's Core credentials. Prefix extraction for explicitly selected non-default Scopes requires the updated Server and Gateway. The normal extraction fallback applies when a prefix is unavailable.

How was this change tested?

Tests: cargo test --locked --manifest-path gateway/Cargo.toml -p pc-gateway-app --test gateway_behaviors request_scope_binds_recall_checks_and_capture -- --exact.
Result: cargo test --locked --manifest-path gateway/Cargo.toml -p pc-gateway-app --test gateway_behaviors — original module passed, 13 tests.
Result: cargo test --locked --manifest-path gateway/Cargo.toml -p pc-gateway-app --test gateway_behaviors request_scope_binds_recall_checks_and_capture -- --exact — base exited 101 and reported two gateway:fallback preparations for four requests. Patched exited 0 with 1 passed.
Result: cargo test --locked --manifest-path gateway/Cargo.toml -p pc-gateway-core --test executor_semantics — 92 passed on base.
Result: cargo test --locked --manifest-path gateway/Cargo.toml --workspace -- --skip live_ — passed after the fix, including all 14 gateway_behaviors tests, 92 executor semantics tests, and both tests in real_core_rpc_recovery. Four live_context_check cases were filtered out.
Result: uv run --locked pytest tests/builtin/artifacts/atomic_memory/test_prefixed_extraction.py -q — 31 passed before and after the change.
Result: uv run --locked pytest tests/builtin/artifacts/atomic_memory/test_prefixed_extraction.py -q -k 'a_gateway_turn_is_extracted_over_its_prefix_without_resending_the_transcript or a_one_pass_window_continues_the_prefix_through_every_step' — both updated cases failed before the Python fix because callback headers were absent. Both passed afterward.
Result: uv run --locked pytest tests/test_capture_content_sources.py -q — 9 passed.
Result: cargo +1.95.0 fmt --manifest-path gateway/Cargo.toml --all --check — passed.
Result: cargo +1.95.0 clippy --locked --manifest-path gateway/Cargo.toml --all-targets -- -D warnings — passed.
Result: uv run --locked ruff check src/powercontext/builtin/artifacts/atomic_memory/{extraction,prefixed_extraction}.py src/powercontext/builtin/runtime/atomic_memory_processing.py tests/builtin/artifacts/atomic_memory/test_prefixed_extraction.py — passed.
Regression coverage: gateway/crates/pc-gateway-app/tests/gateway_behaviors.rs.

AI usage statement

OpenAI Codex (GPT-6) implemented the change, ran the local checks, and authored this PR description.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
@AlexStocks AlexStocks added the 1.6.1 v1.6.1 release tracking label Nov 24, 2026

@NeverENG NeverENG 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.

Reviewed efc2cc4. No blocking findings in explicit Scope selection or the extraction callback. Recall, send/release checks, capture, prefix lookup, and the Python callback carry the selected Scope consistently; ambiguous selections are rejected.

@hidb4ai

hidb4ai commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

please resolve conflicts

@hidb4ai

hidb4ai commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed commit 1502599. No new correctness issue found in the Scope-routing changes, but merge readiness is still blocked:

  • The current branch conflicts with master in README.md, README_CN.md, and README_JP.md; the previous merge no longer resolves the current base.
  • SQLite acceptance failed because Run deterministic scenarios exceeded its 40-minute timeout, and the aggregate CI result is failing. This log does not establish that Scope routing caused the timeout.

Please reconcile the current master conflicts and investigate/re-run the failed acceptance check on the updated commit before merging.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
@hidb4ai

hidb4ai commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed 5bdaeb5. The previous merge conflicts are resolved, and I found no new correctness issue in Scope routing. Live Core/Gateway checks with a real model passed for cross-Scope Chat recall/capture and for non-default-Scope Responses prefix extraction; this validation used isolated SQLite.

CI is still blocking merge: SQLite acceptance and the aggregate CI result failed. The uploaded LoCoMo evidence reports AgentSetupTimeoutError: Agent setup timed out after 360.0 seconds, leaving the business scenarios incomplete. This is different from the earlier job-level timeout and does not demonstrate a Scope-routing regression. Please resolve the agent setup failure and obtain a passing acceptance run on the updated branch before merging.

@frf12

frf12 commented Nov 26, 2026

Copy link
Copy Markdown
Member

CI infrastructure follow-up: #2827 is now merged into master and routes dependency downloads inside Harbor task containers through the shared cache. The latest Harbor CI on this branch used the older workflow and timed out during dependency setup.

Please merge the latest master into this branch, resolve any conflicts, and push to start a new Main run. Rerunning the existing workflow attempt keeps its original workflow, so it will not pick up this fix.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
Brings in oceanbase#2859/oceanbase#2846 and the Harbor shared-download CI fix (oceanbase#2827) so the
next acceptance run exercises the updated install path.

Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>

@Fengzdadi Fengzdadi 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.

Reviewed d9fb44e. No blocking concerns.

What I checked:

  • Scope isolation of the gateway's per-conversation state. The replay cache matches on (scope_id, conversation_id, anchor) (replay_cache.rs:81, :89, :105), PrefixStore::get requires the stored Scope (prefix_store.rs:116), and turn_digest hashes the Scope together with the conversation (executor.rs:1342). I also ran an uncommitted test in the gateway_behaviors harness: a tool turn in one Scope, then the same continuation with a second Scope, then with the first again. I ran it on Chat and Responses, once with a conversation header and once with the ID that #2813 infers. The second Scope got no replayed block and its own capture digest, and the first Scope's continuation replayed its context.
  • Header handling. Repeated, empty, comma-separated, over-256-byte and non-UTF-8 values return 400 invalid_scope_id, and the header is not forwarded upstream.
  • Mixed versions. An older Server sends no Scope on the extraction callback, so a handle from a non-default Scope is not found, the gateway returns prefix_unavailable, and extraction falls back to the normal path. An older Gateway ignores the header. Neither direction errors.
  • Tests. request_scope_binds_recall_checks_and_capture fails with the base ingress.rs, and three of the new test_prefixed_extraction.py tests fail with the base sources.

Three optional notes, all docs:

  1. Trust boundary (docs/en/docs/operate/deploy-server.md:190, zh :192). "Use a Gateway with clients trusted to select the Scopes available to its Core credential" is accurate, but the consequence could be spelled out. Any client with the gateway key can now have the gateway recall from and capture into every Scope that PC_GATEWAY_CORE_TOKEN can reach, or every Scope when Core does not enforce access. On a loopback listener without PC_GATEWAY_API_KEY, that client is any local process. Before this change those clients reached only PC_GATEWAY_DEFAULT_SCOPE, so an upgrade note would help operators who relied on that default as a boundary.
  2. Codex guide (docs/en/docs/integrations/codex.md:415-418, zh :360-361). The PC_GATEWAY_DEFAULT_SCOPE bullet ends at "to print that Scope" and no longer says what to do with it, and the provider block sends no Scope header, so a Codex user still lands in the fallback Scope. Codex's [model_providers.<id>] accepts http_headers and env_http_headers (both present in Codex 0.167.2), so the provider block could carry http_headers = { "X-PowerContext-Scope-ID" = "<scope>" }.
  3. Mixed versions in the docs. The PR body says prefix extraction for a non-default Scope needs both the updated Server and the updated Gateway. "Memory extraction over the cached prefix" (deploy-server.md:235) does not say so yet. One sentence would do; the fallback itself is safe.

The other gateway guides still say the gateway uses only PC_GATEWAY_DEFAULT_SCOPE: Deep Agents, LangChain, LangGraph, OpenClaw and Pydantic AI, and the Pi, Hermes and Bub guides in #2755, #2829 and #2836. I'll update them after this merges, along with the Codex example from note 2 if it isn't in this PR by then.

@hidb4ai

hidb4ai commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed d9fb44e against 69c28d4. The blockers in my previous comment are resolved: the branch is mergeable, SQLite acceptance and the aggregate CI result now pass. I found no blocking correctness issue in the Scope-routing changes.

Live tests with the current Core/Gateway and a real model passed for three-Scope Chat capture/recall isolation, headerless default behavior, injection-off control, persistence and recall after restart, and non-default-Scope prefix callbacks through both Chat/per-candidate and Responses/one-pass extraction. These checks used isolated SQLite; they do not establish OceanBase or native Codex/OpenCode host acceptance.

No database tables or migrations are added. This does add a request-header contract (X-PowerContext-Scope-ID) and callback routing metadata. The documented trust boundary remains important: clients select among Scopes accessible to the Gateway's Core credential. Merge-readiness score: 88/100. I am leaving this as review feedback rather than an approval because the new interface falls outside my automatic-approval criteria.

zhanghuidinah pushed a commit that referenced this pull request Nov 30, 2026
Since #2810 a request selects its Scope with X-PowerContext-Scope-ID and
falls back to PC_GATEWAY_DEFAULT_SCOPE, but the LangChain, LangGraph,
Deep Agents, Pydantic AI, Hermes, OpenClaw, Dify and Bub gateway guides
still said the gateway works in one Scope. Each guide now says how its
host or application can send the header, what requests without it use,
that the gateway's Core credential needs access to every Scope requests
select, and links the Gateway request Scope trust boundary.

Part of #2250.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.6.1 v1.6.1 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(gateway): resolve the Scope per request instead of one PC_GATEWAY_DEFAULT_SCOPE

6 participants