Skip to content

feat(integrations): use host-independent workspace Scope binding keys across hosts #2879

Todo

Description

@wycocoyu

Feature description

Allow a single workspace-to-Scope binding to apply across all PowerContext host integrations (Claude Code, Codex, OpenCode, Pi, DSH, WorkBuddy, Kimi Code, ZCode, Hermes, and Antigravity in #2871) when working on the same repository checkout, instead of keying durable workspace bindings by host integration.

Problem and proposed solution

Problem

Today, host plugins include integration inside the workspace binding key when calling /v1/scope-bindings/resolve and /v1/scope-bindings:

  • In src/powercontext_integration_lifecycle/scope_binding.py:170-174 (and the generated copies in Claude Code, Codex, and Antigravity), workspace_binding_key() produces:
    {"integration": host.integration, "kind": "workspace", "external_id": external_id}
  • Equivalent host integrations follow the same per-integration pattern, including integrations/opencode/plugins/powercontext/src/scope.ts, integrations/dsh/plugins/powercontext/src/scope.ts, integrations/pi/plugins/powercontext/src/scope.ts, integrations/kimi-code/plugins/powercontext/shared/runtime.mjs, integrations/workbuddy/plugins/powercontext/scripts/workspace_scope.py, and integrations/hermes/plugins/powercontext/provider.py.

Because integration is part of the key:

  1. If a user creates a dedicated Scope for Repository A and binds it in Codex (("codex", "workspace", <sha256>)), opening the exact same checkout in Claude Code or Antigravity sends ("claude-code", "workspace", <sha256>) or ("antigravity", "workspace", <sha256>), misses the binding, and silently falls back to the Server's Default Scope (allow_default=True).
  2. Switching hosts on the same repository loses recall from the repository Scope and writes new Sources/Artifacts into Default instead, splitting repository context across Scopes and forcing O(repos × hosts) manual re-binding.
  3. Note that this is not asking to derive scope_id automatically from directory paths (RFC 1345 deliberately forbids path-derived Scopes). It is specifically about sharing the explicitly bound workspace lookup key across hosts on the same checkout.

This directly aligns with #2250 (which marked "Changing how workspace keys are derived" as a Non-goal for follow-up) and complements #2868 / #2875 (which resolves linked Git worktrees to the main checkout path within a single host, while still keeping host.integration in the binding key).

Proposed solution

  1. Host-independent workspace binding key namespace: Introduce a shared workspace binding key identity (decoupled from host.integration) as an RFC follow-up to RFC 1345, while preserving the invariant that workspace keys remain lookup inputs only and never generate scope_id. Pair cleanly with bug(integrations): a bound checkout's Scope does not apply in its linked Git worktrees #2868's canonical checkout identity so linked worktrees across any host resolve to the same Scope.
  2. Backward-compatible resolution precedence: Update binding_keys() / /v1/scope-bindings/resolve lookup order with deterministic precedence — for example: explicit_scope_id -> session binding -> optional host-specific workspace override (if present) -> host-independent shared workspace binding -> legacy per-host workspace binding -> configured default_scope_id (preserving 409 scope_binding_target_missing stop-on-conflict invariants).
  3. One-shot binding CLI / helper: Update powercontext scope and host scope_binding.py --bind-scope flows so binding a repository once covers every installed PowerContext host integration on that machine (unless an operator explicitly requests a host-scoped override).

Alternatives considered

  1. Manually bind every repository across every installed host (O(repos × hosts)): High maintenance friction and error-prone whenever a user adds a new host or forgets one binding, causing silent fallback to Default and cross-repo/split-brain contamination.
  2. Set POWERCONTEXT_<HOST>_SCOPE_ID environment variables: Overrides workspace bindings globally across all repositories rather than per checkout, unless wrapped in external directory tools like direnv.

Additional context

中文说明

功能描述

支持将同一仓库目录的工作区 Scope 绑定共享给所有 PowerContext 宿主集成(包括 Claude Code、Codex、OpenCode、Pi、DSH、WorkBuddy、Kimi Code、ZCode、Hermes 以及 #2871 中的 Antigravity),而不再按宿主 integration 名称分别隔离工作区绑定键。

问题背景与建议方案

问题描述

目前各宿主插件在调用 /v1/scope-bindings/resolve 与 /v1/scope-bindings 时,都会把客户端名称 integration 写进 workspace 绑定键:

  • 在 src/powercontext_integration_lifecycle/scope_binding.py:170-174(以及生成同步到 Claude Code、Codex、Antigravity 的副本)中,workspace_binding_key() 构造的是:
    {"integration": host.integration, "kind": "workspace", "external_id": external_id}
  • 其他宿主实现也沿用了同样按宿主隔离的模式,包括 integrations/opencode/plugins/powercontext/src/scope.ts、integrations/dsh/plugins/powercontext/src/scope.ts、integrations/pi/plugins/powercontext/src/scope.ts、integrations/kimi-code/plugins/powercontext/shared/runtime.mjs、integrations/workbuddy/plugins/powercontext/scripts/workspace_scope.py 与 integrations/hermes/plugins/powercontext/provider.py。

由于绑定键耦合了 integration 字段,会产生以下实际痛点:

  1. 用户为仓库 A 新建独立 Scope 并在 Codex 中完成绑定(("codex", "workspace", <sha256>))后,若改用 Claude Code 或 Antigravity 打开同一本地仓库,发出的键变成 ("claude-code", "workspace", <sha256>) 或 ("antigravity", "workspace", <sha256>),服务端查不到绑定,会静默回落到全局 Default Scope(allow_default=True)。
  2. 在同一仓库切换不同宿主工作时,不仅无法召回原仓库 Scope 的历史记忆,新捕获的 Source/Artifact 还会写入 Default,造成同一项目记忆被拆散且需要按 O(仓库数 × 宿主数) 重复手工绑定。
  3. 需要明确的是,本提案并不要求按目录路径自动创建或派生 scope_id(RFC 1345 已明确禁止路径派生 Scope),而是希望在用户显式完成「仓库 -> Scope」绑定后,工作区查询键能跨宿主通用。

这与 #2250(在其 Non-goals 中单独列出后续再改 workspace key 派生方式)精准衔接,并与 #2868 / #2875(解决同一宿主下 linked Git worktree 找不到主仓库绑定的问题,但仍保留 host.integration 隔离)形成互补。

建议方案

  1. 定义与宿主无关的工作区绑定键命名空间:作为 RFC 1345 的后续扩展 RFC,引入不绑定 host.integration 的共享工作区绑定键;同时继续严格保持「工作区键仅作查询输入、绝不派生 scope_id」的设计不变量,并与 bug(integrations): a bound checkout's Scope does not apply in its linked Git worktrees #2868 的主仓库规范路径标识协同,使多宿主与 linked worktree 打开同一仓库都能命中同一 Scope。
  2. 向后兼容的解析优先级:在 binding_keys() 与 /v1/scope-bindings/resolve 中保留清晰可预期的回退顺序,例如:explicit_scope_id -> session 绑定 -> 可选的单宿主显式覆盖绑定(如有) -> 与宿主无关的共享工作区绑定 -> 遗留的单宿主工作区绑定 -> default_scope_id(同时继续严格遵守 409 scope_binding_target_missing 遇损即停不回退的不变量)。
  3. 一次绑定多端生效的 CLI 与辅助脚本:更新 powercontext scope 与插件自带 scope_binding.py --bind-scope,让用户在同一仓库执行一次绑定即可覆盖本机全部已安装宿主(除非运维明确需要为特定宿主单独覆盖)。

备选方案考虑

  1. 在每个已安装宿主上重复绑定同一批仓库(O(仓库数 × 宿主数)):维护成本随宿主数量线性增长,新增或漏绑一个客户端就会静默掉回 Default,导致跨项目串库与记忆分裂。
  2. 设置 POWERCONTEXT_<HOST>_SCOPE_ID 环境变量:属于进程/全局级覆盖,无法按本地不同仓库自动区分,除非额外依赖 direnv 等外部工具按目录切换环境变量。

补充上下文

Activity

  1. wycocoyu commented on Nov 27, 2026

    @wycocoyu
    ContributorAuthor

    This issue is pending evaluation.

    中文说明

    这个 issue 待评估。

  2. Fengzdadi commented on Nov 27, 2026

    @Fengzdadi
    Collaborator

    @wycocoyu The problem is real, and the direction is right. The key has carried integration since Scope bindings were first implemented (#1401), so this is a gap in the original design and not a regression. Here is a design that closes it. It needs no Server, API, or storage change. I checked it against master at 389c440.

    One correction first: dropping integration is not enough

    The issue assumes every host computes the same <sha256> for a checkout. They do not:

    Hosts Directory that is hashed Symbolic links
    Claude Code, Codex, WorkBuddy, ZCode Git top level, or the directory itself outside Git resolved
    Kimi Code, Hermes, Bub working directory resolved
    DSH, OpenCode, Pi working directory kept (path.resolve)

    Codex and Claude Code already agree. Codex and Pi agree only when Pi starts at the repository root through a path without symbolic links. On macOS with Git 2.50.1, I hashed one checkout three ways: the Git top level, path.resolve of the same checkout reached through a symbolic link, and path.resolve of a subdirectory. The three IDs differ.

    So the work has two parts: one identity rule that every host computes, and a shared namespace for it.

    1. One workspace identity

    dir  = the directory the host reports for the session
    root = `git rev-parse --show-toplevel` in dir, or dir itself when it is not in a work tree
    path = the real path of root: absolute, symbolic links resolved, encoded as UTF-8
    id   = lowercase hex SHA-256 of path
    
    • The unit is the checkout, which is what the issue asks for. A subdirectory of a repository is the same workspace. Outside Git, each directory is its own workspace.
    • Claude Code, Codex, WorkBuddy, and ZCode already compute this value. For them, the shared ID equals the ID they send today. Windows is the case I have not verified (see the tests below).
    • The rule lives once per language in the lifecycle modules, beside the Git helper that fix(integrations): resolve linked Git worktrees to their main checkout's workspace binding #2875 adds. That helper already asks Git for the top level in one call, so no host starts an extra process.

    2. A shared key beside the host's own key

    {"integration": "shared", "kind": "workspace", "external_id": "<id>"}

    ScopeBindingKey.integration is a free string, and POST /v1/scope-bindings/resolve tries binding_keys in order and returns the first key that has a binding. A host therefore sends the shared key in the list it already sends. #2875 uses the same mechanism for linked worktrees. shared becomes a reserved name that no host may use as its ID.

    3. Resolution order

    1. The explicit Scope setting.
    2. The session binding.
    3. This checkout: the shared key, then the host's own key.
    4. In a linked worktree, the main checkout: the shared key, then the host's own key.
    5. The Server's default Scope.

    The request stays a single call with at most five keys, and the Server makes one lookup per key until the first match. Because the Server is unchanged, a binding whose target is missing still stops resolution with 409 scope_binding_target_missing.

    The issue lists "host-specific override" and "legacy per-host binding" as two levels. They are the same stored key, (codex, workspace, <id>), so the Server cannot tell them apart and they take one position. I put that position after the shared key for the reason above. A host that must use a different Scope sets its explicit Scope setting, for example POWERCONTEXT_CODEX_SCOPE_ID, which is first in the order. If a per-host override for one checkout turns out to be needed, a new key kind ahead of the shared key adds it without touching stored bindings.

    4. Bind and clear

    • Bind writes the shared key for this checkout. In a linked worktree it writes the worktree's own key, as in fix(integrations): resolve linked Git worktrees to their main checkout's workspace binding #2875.
    • Clear removes the shared key and this host's own key for this checkout. Removing only the shared key would bring this host's old binding back.
    • powercontext scope resolve computes the shared key with the one rule. Its per-host table of derivations (_workspace_binding_external_id) remains only for the hosts' own keys. powercontext scope binding set gains an option that takes a directory, so nobody computes a hash by hand.
    • The resolve response is the Scope descriptor. It does not say which key matched. Each host's Scope diagnostic should name the matching key, by resolving the keys one at a time with allow_default off. This runs in diagnostics only, not in hooks.

    5. Existing bindings

    Nothing is migrated or rewritten.

    • An existing per-host binding keeps working for its host until the checkout gets a shared binding.
    • Making it visible to the other hosts takes one bind per checkout, instead of one per checkout and host.
    • Another host's old key for the same checkout stays in storage. It is reachable only while the checkout has no shared binding, for example after a clear made from a different host. The diagnostic in section 4 shows that case.
    • A new host, such as Antigravity in feat(integrations): add Antigravity (AGY) host plugin support #2871, sends only the shared key.

    During the rollout, a host that has not been updated does not read the shared key, so it behaves as it does today.

    6. What stays as it is

    • A workspace key only finds a binding. It never derives a scope_id, as RFC 1345 requires.
    • The identity is a local path. A second clone, or the same repository on another machine, is a different workspace and needs its own bind.
    • A workspace without a binding still resolves to the default Scope without a notice. This design reduces how often that happens but does not remove it.

    7. Alternatives I rejected

    • Git remote URL as the identity. It would follow a repository across clones and machines. But not every directory has a remote, some have several, URL forms vary, and two clones that are bound to different Scopes on purpose would become one workspace.
    • Writing one binding per installed host. The keys still differ per host, the derivations still disagree, and a host installed later needs another bind.
    • Making the Server ignore integration for workspace keys. It changes the meaning of stored keys and the API contract, and the derivations still disagree.

    8. Order of work and tests

    1. fix(integrations): resolve linked Git worktrees to their main checkout's workspace binding #2875 merges first. It changes the same functions and adds the helper this design builds on.
    2. A short RFC records the identity rule, the reserved name, the order, and the bind and clear behavior.
    3. The rule and shared test vectors land with the Python lifecycle package (Claude Code, Codex), powercontext scope, and Codex session import. Then the TypeScript module (DSH, OpenCode, Pi). Then Kimi Code, ZCode, WorkBuddy, Hermes, and Bub.

    Tests that matter:

    • The Python and JavaScript implementations hash the same real directories and must return the same ID, as tests/test_git_worktree.py in fix(integrations): resolve linked Git worktrees to their main checkout's workspace binding #2875 does for the Git helper. Windows needs its own run: drive-letter case and path separators are where the two languages can disagree, and I have not checked them.
    • For each host: with no shared binding, the resolved Scope equals the one before the change.
    • One run with a real Server and two real hosts from different rows of the table above, for example Codex and Pi: bind in one, resolve in the other.
  3. added
    module:core
    on Dec 2, 2026
  4. moved this from Todo to In progress in PowerContexton Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

module:core

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions