Skip to content

feat(dsh): send the prompt's Scope to the gateway provider - #2965

Open
Fengzdadi wants to merge 1 commit into
oceanbase:masterfrom
Fengzdadi:feat/dsh-gateway-scope-header
Open

Fengzdadi wants to merge 1 commit into
oceanbase:masterfrom
Fengzdadi:feat/dsh-gateway-scope-header

Conversation

@Fengzdadi

Copy link
Copy Markdown
Collaborator

Which issue or RFC does this PR close?

Part of #2250.

Follows #2873 (merged), which lets DeepSeek Harness (DSH) leave prompt-time context to the PowerContext gateway.

Rationale for this change

Behind the gateway, DSH's turns are prepared, checked, and captured in the one Scope set by PC_GATEWAY_DEFAULT_SCOPE, while the plugin already resolves a Scope for each workspace and uses it for prompt capture and MCP tools. Sending that Scope as X-PowerContext-Scope-ID lets the gateway work in the same Scope as the rest of the plugin, so one gateway serves several workspaces. This follows Pi (#2893, POWERCONTEXT_PI_GATEWAY_PROVIDER) and OpenCode (#2903).

What changes are included in this PR?

  • A new setting names the llm-pi-ai provider that routes through the gateway: the patch setting gatewayProvider (a string), or POWERCONTEXT_DSH_GATEWAY_PROVIDER, which takes precedence. Only requests whose provider equals it get the Scope header, so a provider that DSH calls directly never receives one.
  • The header is sent only while prompt recall is off, through the same llm-pi-ai/request-headers hook that sends the conversation and internal-turn headers. With recall on, the hook is not registered, as before.
  • The header carries the Scope that the session's latest prompt resolved in agent/pre-step. The request hook is synchronous and makes no network call; every request of a prompt reuses that prompt's resolution.
  • Internal requests (compaction, session title, auto review) also carry the Scope header, as in Pi, so Core checks them in the same Scope as the session's turns. They still carry X-PowerContext-Turn: internal and no conversation header.
  • When the session's latest prompt resolved no Scope, or resolved one whose ID the header cannot carry, the request carries no Scope header, so the gateway uses PC_GATEWAY_DEFAULT_SCOPE. The gateway accepts one ID of 1–256 visible ASCII characters without a comma and answers HTTP 400 invalid_scope_id otherwise, while Scopes from early releases can have other IDs; a non-Latin-1 ID would also make Headers.set throw inside the hook. This follows _scope_headers in the prefixed extraction (feat(gateway): bind model requests to an explicit Scope #2810). A prompt that resolved none does not reuse an earlier prompt's Scope. The plugin logs one content-free gateway_scope diagnostic (outcome: skipped, reason: scope_unresolved or scope_id_unsupported, never the ID) per such prompt, not one per request. Scope resolution, prompt capture, /pc and MCP tools still use the resolved Scope.
  • The conversation header and the internal marker work as before.
  • The DSH guide (en and zh) and the plugin README document the setting:
    • which requests carry the header and when it falls back to the default Scope;
    • that, when Core enforces authentication, the gateway's Core credential needs scope.contributor on each workspace Scope the plugin sends, in the grant list the guide already gives for PC_GATEWAY_DEFAULT_SCOPE and every Scope a request selects;
    • that the gateway key does not limit which Scope a request selects: any client that can call the gateway can select any Scope that the gateway's Core credential can access, with a link to the Server guide's "Gateway request Scope".
  • lib/index.js and lib/index.d.ts are rebuilt.

Are there any user-facing changes?

Yes. POWERCONTEXT_DSH_GATEWAY_PROVIDER / gatewayProvider is new and unset by default. While it is unset, DSH sends no Scope header, as before. Once it is set and prompt recall is off, the gateway prepares, checks, and captures in each workspace's Scope, so when Core enforces authentication the gateway's Core credential needs scope.contributor on every such Scope as well as on PC_GATEWAY_DEFAULT_SCOPE. No breaking changes.

How was this change tested?

New behavior tests in integrations/dsh/plugins/powercontext/tests/automatic-path.spec.ts cover:

  • With recall off, requests to the named provider carry the Scope, internal requests included. The setting works from both the patch and the environment, and the environment overrides the patch.
  • Requests to any other provider carry no Scope header, and the prompt makes only Scope resolution and capture requests.
  • Without a named provider, or with recall on, no request carries the Scope header.
  • With no Scope resolved, no request carries the header, and the plugin logs exactly one gateway_scope diagnostic per prompt, with no prompt text in it.
  • A Scope ID with a comma, a space, non-ASCII Latin-1 text, non-Latin-1 text, or more than 256 characters gets no Scope header and no exception; the conversation header and the internal marker are unchanged, capture still uses the Scope, and the plugin logs one scope_id_unsupported diagnostic per prompt without the ID. IDs at the edges of the accepted syntax (!, ~, 256 characters) are still sent.
  • A later prompt that resolves no Scope drops the previous one.
  • A non-string gatewayProvider is rejected.

Commands run on 504179c2c (this commit on master 818467a50), offline, macOS:

npm_config_offline=true pnpm --dir integrations/dsh/plugins/powercontext test        # 1223 passed
npm_config_offline=true pnpm --dir integrations/dsh/plugins/powercontext test:e2e    # 9 passed
npm_config_offline=true pnpm --dir integrations/dsh/plugins/powercontext build       # no diff to lib/ or dsh-pet/lib/
node --test integrations/dsh/plugins/dsh-pet/tests/adapter.test.mjs integrations/dsh/plugins/dsh-pet/tests/package.test.mjs   # 5 passed
(cd integrations/dsh/plugins/powercontext && ./node_modules/.bin/tsdown --config ../../../cindy/tsdown.config.ts)   # Cindy worker.cjs unchanged
node --test integrations/cindy/tests/*.test.cjs                                     # 26 passed
uv run python integrations/cindy/scripts/package.py                                  # exit 0
.venv/bin/python scripts/generate_integration_lifecycle.py --check                   # passed
uv run pytest tests/test_ci_changes.py                                               # 185 passed
uv run prek run --files <changed files>                                              # passed, ty check included

These tests drive the plugin's DSH hooks with fixtures. tests/e2e has no test of DSH behind the gateway.

Real-host run on 504179c2c: DSH 0.2.2-rc.5 (--profile headless, @deepseek-ai/dsh-llm-pi-ai 0.2.2-rc.5) with the plugin's lib/ built at that commit, through a Server and a pc-gateway built from it (cargo build --locked --offline), to a local stub Responses upstream. No model provider was called.

  • Core: SQLite over UDS, access enforced, protection enforcing, one target.
  • Gateway: the combined deployment, set up as the guide shows: PC_GATEWAY_UPSTREAM_URL_RESPONSES at the stub, PC_GATEWAY_UPSTREAM_PROFILE naming the target, PC_GATEWAY_SEDIMENT=on, and PC_GATEWAY_DEFAULT_SCOPE. PC_GATEWAY_CORE_TOKEN was the Server's bootstrap token.
  • Scopes: the workspace Scope (bound to the workspace) and PC_GATEWAY_DEFAULT_SCOPE were different Scopes, each holding a distinct synthetic fact.
  • Plugin: POWERCONTEXT_DSH_PROMPT_RECALL=false, POWERCONTEXT_DSH_CAPTURE_PROMPTS=false and POWERCONTEXT_DSH_GATEWAY_PROVIDER=powercontext-gateway, using the guide's powercontext-gateway provider entry.

Three prompts went to powercontext-gateway in one session: a new session, a follow-up, and a prompt that the stub answered with an mcp__powercontext__search_memory call whose scope_id it copied from the Scope routing in DSH's runtime context. A recording proxy in front of the gateway showed:

  • Gateway provider: all five model requests carried X-PowerContext-Scope-ID with the workspace Scope that the plugin's binding resolution returned, the same Scope that the routing names. So in rc.5, request.provider in llm-pi-ai/request-headers is the provider's key under providers.
  • Agent loop: the four agent-loop requests also carried X-PowerContext-Conversation-ID.
  • Session title: the session-title request carried X-PowerContext-Turn: internal and no conversation header.
  • Gateway behavior: Core's prepare diagnostics record the three prompts in the workspace Scope. The gateway injected the workspace fact into the first and third prompts and sent no default-Scope fact upstream. It captured the three turns in the workspace Scope and none in the default Scope. The search call with the routed scope_id returned the workspace fact.
  • Diagnostics: the plugin logged no gateway_scope diagnostic.
  • Recall on: one more prompt in a new session with recall on and the provider still named; no model request carried any X-PowerContext-* header. As the guide describes, Core rejected the agent-loop requests, which carry the plugin's prepared-context block, and the gateway returned HTTP 503. The session-title request, the only one that reached the upstream, was captured in PC_GATEWAY_DEFAULT_SCOPE.

Not covered by a real run:

  • Compaction and auto-review requests: none occurred, so the Scope header on them is covered only by the tests above.
  • A Scope ID that the header cannot carry, and an unresolved Scope: covered only by the tests.
  • A provider that DSH calls directly while the gateway provider is named: covered by the tests here. It was also checked in an earlier real run of this change on an older base, with a gateway set up for feat(gateway): bind Core-approved target to upstream sends #2630's model destination, which revert(gateway): fully revert #2630 upstream target binding #2920 reverted. That run is not repeated here.
  • A gateway Core credential that holds only the listed roles, and what a request that selects a Scope without them receives: the run used the Server's bootstrap token.
  • Web mode (dsh web) and a real model provider.

AI usage statement

Claude Code (Claude Opus 5.5) wrote the change, ran the checks listed above, and wrote this description. It reviewed the diff twice, once for the implementation and once for users and the documented contract, and checked each claim in this description against the diff, the tests, and the run's recorded requests. After #2920 reverted #2630, it moved the guide's grant and trust text to master's gateway setup. After #2873 merged, it rebased the change onto master and reran the checks and the real-host run above on the rebased commit. Another Claude Code session (Claude Opus 5.5) reviewed the change, the rebase and this description before the PR was opened, without rerunning the checks.

@hidb4ai hidb4ai added module:core Core: PR changes under src/ module:gateway Primary module: model gateway, protocols and context injection and removed module:core Core: PR changes under src/ labels Dec 2, 2026

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

Reviewed 504179c. The provider-scoped header and documented fallback are reasonable; no new database tables. Focused tests, plugin HTTP acceptance, and a real DSH 0.2.2-rc.5 → current Gateway → OceanBase/real-provider run passed, with workspace Scope routing and capture isolated from the default Scope. The real run used disabled protection and did not exercise compaction, auto-review, or a successful MCP tool call. Merge score: 80/100.

@hireview001

Copy link
Copy Markdown
Collaborator

The affected modules are Gateway. @frf12, please coordinate follow-up. Gateway: @frf12 @happy-v587 @NeverENG. Please coordinate the review of the affected changes.

@hireview001
hireview001 requested a review from frf12 December 2, 2026 18:41
// A request without a session ID cannot be matched to a prompt's resolution.
if (!sessionId) return { scopeId: undefined, attempt: 0 }
const key = keyFor(sessionId, undefined)
return { scopeId: cache.get(key)?.scopeId, attempt: attempts.get(key) ?? 0 }

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.

[P2] Keep the gateway Scope fixed to the prompt resolution

If pre-step Scope resolution fails but a later MCP tool retry succeeds, forTool repopulates this cache and subsequent gateway requests for the same prompt switch from PC_GATEWAY_DEFAULT_SCOPE to the workspace Scope. Snapshot the pre-step result separately so tool retries cannot change the Scope used for that prompt's preparation, checks, and capture.

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.

Re-review of 504179c2cbb974fe8e7379e2d5f4ed38197ba301: this P2 remains in the current source and published bundle. The concrete state transition is:

  1. forPreStep increments the prompt attempt, deletes the cache entry, and fails to resolve a Scope. forGateway then returns no Scope, so the first model request uses the gateway default.
  2. registerMcpPolicy later calls forTool for the same session. A successful retry writes the workspace Scope into that same cache without starting a new prompt attempt.
  3. forGateway at lines 126–130 now returns the workspace Scope, so the next model request of the same prompt selects a different Scope.

This splits recall replay, admission checks, and turn capture across Scopes within one tool loop. Please retain an independent pre-step snapshot for gateway routing, including an unresolved result, and add a regression covering failed pre-step resolution followed by successful MCP resolution. The tool's recovery must not mutate the prompt's gateway snapshot. This is a source-level execution trace; I did not rerun a host reproduction in this re-review.

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

Re-review report — host integration

Reviewed head: 504179c2cbb974fe8e7379e2d5f4ed38197ba301.

Result: one P2 finding remains; fix and re-review recommended.

Gateway routing still reads the same mutable Scope cache that MCP tool recovery populates. If pre-step resolution fails, the first model request uses the gateway default Scope; a successful MCP resolution can then move subsequent requests of that prompt to the workspace Scope. Preserve a separate pre-step routing snapshot, including failure. Details and the exact execution trace are in the existing finding thread.

Reviewed configuration, Scope resolution and MCP callers, provider header filtering, published bundle, documentation, and relevant tests. Existing CI for this head passed 1,223 package tests and runtime/package checks. Those checks do not establish a real DSH-to-Gateway acceptance for the newly added Scope header.

Validation scope: I inspected the current source, complete PR diff, existing review threads, and CI logs for this head. No new local test run was performed in this re-review. The cited CI ran a test merge with its then-current base; it is not proof of a merge with the latest master. This is a comment-only report, not an approval or an unconditional merge recommendation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:gateway Primary module: model gateway, protocols and context injection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants