Skip to content

feat(integrations): share workspace Scope bindings across hosts - #2988

Draft
wycocoyu wants to merge 7 commits into
oceanbase:masterfrom
wycocoyu:feat/shared-workspace-scope-2879
Draft

wycocoyu wants to merge 7 commits into
oceanbase:masterfrom
wycocoyu:feat/shared-workspace-scope-2879

Conversation

@wycocoyu

@wycocoyu wycocoyu commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Which issue or RFC does this PR close?

Closes #2879.
Implements RFC 2988-shared-workspace-scope-bindings.

Rationale for this change

Today, each workspace-aware host integration binds and resolves workspace Scopes under its own integration name (claude-code, codex, opencode, pi, dsh, workbuddy, zcode, kimi-code, hermes, bub), and derives workspace paths using three incompatible rules (git root + realpath, cwd + realpath, and cwd + path.resolve). Binding a repository checkout to a project Scope in one host leaves every other host on the same machine falling back to the Server default Scope (O(repositories × hosts) manual bindings).

In line with RFC 1345 (opaque Server-owned scope_ids, never derived from paths or Git remotes), this PR introduces a canonical cross-host workspace binding under the reserved integration name shared with zero Server schema, HTTP API, or persistence table changes.

What changes are included in this PR?

This Draft PR is organized in layered commits (bilingual RFC -> shared Python/TS lifecycle + CLI -> host integrations + cross-host E2E -> cross-platform identity, routing cache & CLI refinement) so the RFC can also be reviewed independently if preferred:

  • Bilingual RFC (docs/{en,zh}/rfcs/2988-shared-workspace-scope-bindings.md) & manifest guard (scripts/integration_manifest.py):
    • Specifies the canonical workspace identity rule, home-directory and filesystem-root guard, resolution precedence (up to 5 keys per resolve request), bind/clear semantics, and explicit platform/migration boundaries.
    • Reserves shared in scripts/integration_manifest.py so host manifests cannot claim it.
  • Cross-language workspace identity & Git worktree lifecycle (src/powercontext_integration_lifecycle/, integrations/lifecycle/git-worktree.ts):
    • Derives sha256(utf8(canonical_root)) for existing directories from the single existing git rev-parse call: aligns Python on macOS (fcntl.F_GETPATH) with Node.js fs.realpathSync.native across case-insensitive APFS and NFD/NFC paths, strips Windows \\?\ / \\?\UNC\ prefixes with uppercase drive letters, decodes Git stdout explicitly as UTF-8 (surrogateescape), skips spawning git when cwd is missing, and guards against $HOME / %USERPROFILE% (os.path.expanduser("~") / os.homedir()) and filesystem roots (_guarded_git_top / guardedGitTop).
    • Sets HostProfile.legacy_workspace_binding = False by default so newly added hosts emit only shared:workspace, while explicitly enabling legacy_workspace_binding = True for CLAUDE_CODE and CODEX and preserving byte-for-byte frozen legacy <host>:workspace derivations across existing workspace hosts.
    • Adds diagnose_scope_binding reporting matched_key, synced across generated copies via scripts/generate_integration_lifecycle.py.
  • All 10 existing workspace hosts, CLI, and session importer:
    • Updates claude-code, codex, workbuddy, hermes, bub, dsh (with per-cwd cached routing metadata and rebuilt integrations/dsh/plugins/powercontext/lib/), opencode (with rebuilt lib/), pi, kimi-code (preserving unknown status on partial multi-key unbind failures), and zcode (same partial-unbind unknown guard): binding writes shared:workspace, resolution queries session -> shared (own) -> legacy host (own) -> shared (main worktree) -> legacy host (main worktree) -> default, clearing removes both shared and the host's legacy key, and host diagnostics (--diagnose, doctor.ts, doctor.mjs) surface matched_key.
    • Updates powercontext scope resolve (supporting --integration shared and all 10 workspace hosts), adds --cwd to powercontext scope binding set (validating existing directories and rejecting non-shared --integration) and clear (requiring an existing directory and clearing shared by default, or shared plus <host> when --integration <host> is given), and updates powercontext import-sessions (session_import.py).
  • Bilingual integration docs & cross-host E2E tests (docs/{en,zh}/docs/integrations/, tests/):
    • Updates English and Chinese documentation across all 10 workspace-aware hosts.
    • Adds Python × Node.js parity tests (including APFS wrong-case, NFC/NFD, symlinks, Windows directory junctions, $HOME/%USERPROFILE% guard, and Windows path vectors) in tests/test_git_worktree.py, CLI --cwd / --help tests in tests/test_cli_scope.py, and real-Server + SQLite 10-host end-to-end tests in tests/e2e/test_shared_workspace_scope_binding.py.

Are there any user-facing changes?

  • Binding a workspace Scope in any supported host integration or via powercontext scope binding set <SCOPE_ID> --cwd <DIR> writes a canonical shared:workspace binding that applies across every workspace-aware host opening the same checkout (including subdirectories, symlinks, and linked Git worktrees without their own override). Active sessions that pin their Scope at startup (such as Codex and OpenCode) keep their current session Scope until a new session starts.
  • Existing pre-upgrade <host>:workspace bindings continue resolving without manual migration until superseded by a shared:workspace binding or cleared via host unbind / powercontext scope binding clear --cwd <DIR> --integration <HOST> (note: on non-UTF-8 Windows code pages such as cp936, Python's Git helper now decodes git rev-parse output as UTF-8 instead of ANSI, so a pre-upgrade binding created from a mojibake non-ASCII Git path requires rebinding once).
  • Host diagnostics (--diagnose in Claude Code / Codex / WorkBuddy, and doctor in DSH, Kimi Code, and ZCode) report matched_key.
  • No breaking changes to Server HTTP/MCP APIs, OpenAPI schemas, or persisted database tables.

How was this change tested?

  • Code quality, manifest, guidance, lifecycle sync, Ruff, and ty checks (verified locally on macOS arm64; macOS fcntl.F_GETPATH APFS case and NFD/NFC parity is verified locally on macOS, while automated CI exercises Linux and windows-latest once out of Draft):
    • make check (All checks passed!, 62 passed)
    • make contract-test (69 passed)
    • make harness-lint (All checks passed!)
  • Python unit, plugin contract, CLI, and real-Server E2E suites:
    • uv run python -m pytest -n auto tests/test_git_worktree.py tests/test_cli_scope.py tests/test_session_import.py tests/claude_code_plugin tests/codex_plugin tests/workbuddy_plugin tests/integrations tests/test_integration_manifest.py tests/test_cli_zcode.py tests/test_cli_workbuddy.py tests/e2e/test_shared_workspace_scope_binding.py tests/e2e/test_codex_service_chain.py tests/e2e/test_zcode_service_chain.py tests/e2e/test_bub_service_chain.py -q (1197 passed, 11 skipped)
  • JavaScript and TypeScript host unit, E2E, typecheck, and bundle suites:
    • pnpm --dir integrations/dsh/plugins/powercontext test (31 files, 1209 passed) and node --test integrations/dsh/plugins/dsh-pet/tests/adapter.test.mjs integrations/dsh/plugins/dsh-pet/tests/package.test.mjs (5 passed)
    • pnpm --dir integrations/opencode/plugins/powercontext test && pnpm --dir integrations/opencode/plugins/powercontext run typecheck (11 files, 175 passed)
    • pnpm --dir integrations/pi/plugins/powercontext test && pnpm --dir integrations/pi/plugins/powercontext run typecheck (18 files, 232 passed)
    • node --test integrations/zcode/plugins/powercontext/tests/*.test.mjs (116 passed, 1 skipped, 0 failed)
    • node --test --test-name-pattern="workspace|scope|doctor|worktree|binding|unbind" integrations/kimi-code/plugins/powercontext/tests/plugin.test.mjs (8 passed, 0 failed)

AI usage statement

Built, reviewed, and verified with assistance from Antigravity (Gemini) and Claude Code.

中文说明

本 PR 关联或关闭哪个 Issue / RFC?

Closes #2879。
实现 RFC 2988-shared-workspace-scope-bindings。

变更背景与原因

当前各工作区感知宿主集成在绑定与解析工作区 Scope 时均各自使用独立的 integration 名称(claude-code、codex、opencode、pi、dsh、workbuddy、zcode、kimi-code、hermes、bub),且工作区路径派生分成三套互不兼容的规则(Git 根目录 + realpath、cwd + realpath、cwd + path.resolve)。用户在任一宿主中将仓库检出绑定到项目 Scope 后,同一台机器上的其他宿主打开同一目录仍会回退到 Server 默认 Scope(手工维护成本为 O(仓库数 × 宿主数))。

遵循 RFC 1345(scope_id 由 Server 拥有且保持不透明,绝不从路径或 Git 远端地址自动派生),本 PR 在零 Server Schema、零 HTTP API、零持久化表结构改动的前提下,使用保留集成名 shared 引入跨宿主规范工作区绑定。

本 PR 包含哪些变更?

本 Draft PR 采用分层提交组织(中英文双语 RFC -> Python/TS 共享生命周期与 CLI -> 各宿主集成与跨宿主 E2E -> 跨平台路径归一、路由缓存与 CLI 精简收敛),便于按需先独立审阅 RFC:

  • 中英文双语 RFC(docs/{en,zh}/rfcs/2988-shared-workspace-scope-bindings.md)与能力清单校验(scripts/integration_manifest.py):
    • 定义规范工作区身份规则、家目录与文件系统根目录守卫、解析优先级(单次解析请求最多 5 个键)、绑定与清除语义以及跨平台与迁移边界说明。
    • 在 scripts/integration_manifest.py 中保留 shared,防止宿主清单占用该名称。
  • 跨语言工作区身份与 Git Worktree 生命周期(src/powercontext_integration_lifecycle/、integrations/lifecycle/git-worktree.ts):
    • 复用单次 git rev-parse 调用对已存在目录计算 sha256(utf8(canonical_root)):在 macOS 上通过 fcntl.F_GETPATH 使 Python 与 Node.js fs.realpathSync.native 在大小写不敏感 APFS 及 NFD/NFC 路径上逐字节一致,在 Windows 上统一剥离 \\?\ 与 \\?\UNC\ 前缀并大写盘符,Git 标准输出显式按 UTF-8(surrogateescape)解码,目录不存在时直接跳过 git 子进程派生,并按 os.path.expanduser("~") / os.homedir() 一致拦截 $HOME / %USERPROFILE% 及文件系统根目录(_guarded_git_top / guardedGitTop)。
    • 将 HostProfile.legacy_workspace_binding 默认值设为 False,使后续新接入宿主仅发送 shared:workspace;仅对 CLAUDE_CODE 与 CODEX 显式设为 True,并在存量工作区宿主中逐字节冻结原有的 <host>:workspace 派生逻辑。
    • 新增 diagnose_scope_binding 输出 matched_key,并通过 scripts/generate_integration_lifecycle.py 同步生成副本。
  • 全量 10 个存量工作区宿主、CLI 与会话导入器:
    • 统一更新 claude-code、codex、workbuddy、hermes、bub、dsh(按 cwd 缓存共享路由元数据并重编 integrations/dsh/plugins/powercontext/lib/)、opencode(含重编 lib/)、pi、kimi-code(多键解绑部分失败时保留 unknown 状态)与 zcode(同上保留部分解绑 unknown 状态):绑定只写 shared:workspace,解析按 session -> shared(当前检出)-> legacy host(当前检出)-> shared(主检出)-> legacy host(主检出)-> default 顺序查询,清除时一并移除 shared 与当前宿主旧键,且诊断入口(--diagnose、doctor.ts、doctor.mjs)统一展示 matched_key。
    • 更新 powercontext scope resolve(支持 --integration shared 及全部 10 个工作区宿主),为 powercontext scope binding set(校验目录存在并拒绝非 shared 的 --integration)与 clear(校验目录存在,默认清除 shared,指定 --integration <host> 时同时清除 shared 与 <host>)新增 --cwd 选项,并同步更新 powercontext import-sessions(session_import.py)。
  • 中英文文档与跨宿主端到端测试(docs/{en,zh}/docs/integrations/、tests/):
    • 同步更新全部 10 个工作区宿主的中英文用户文档。
    • 在 tests/test_git_worktree.py 新增 Python × Node.js 一致性测试(覆盖 APFS 大小写偏差、NFC/NFD、软链、Windows 目录联接 junction、$HOME/%USERPROFILE% 守卫与 Windows 路径向量),在 tests/test_cli_scope.py 新增 --cwd / --help 测试,并在 tests/e2e/test_shared_workspace_scope_binding.py 新增覆盖全部 10 个宿主的真实 Server + SQLite 端到端测试。

是否存在用户可见变更?

  • 在任一受支持宿主集成内执行工作区 Scope 绑定,或运行 powercontext scope binding set <SCOPE_ID> --cwd <DIR>,均会写入规范 shared:workspace 绑定,并在打开同一检出目录的所有工作区宿主(含子目录、软链及未单独覆盖绑定的关联 Git Worktree)中自动共享生效。启动时固定会话 Scope 的宿主(如 Codex 与 OpenCode)在已开启会话中保持原会话 Scope,新开会话采用新的工作区绑定。
  • 升级前已存在的 <host>:workspace 绑定无需手工迁移即可继续回退生效,直至被新的 shared:workspace 绑定覆盖,或通过宿主解绑 / powercontext scope binding clear --cwd <DIR> --integration <HOST> 清除(注:在非 UTF-8 代码页如 cp936 的 Windows 上,Python 的 Git 辅助函数现按 UTF-8 而非系统 ANSI 解码 git rev-parse 输出,此前由非 ASCII 乱码路径建立的旧绑定重新绑定一次即可)。
  • 宿主诊断入口(Claude Code / Codex / WorkBuddy 的 --diagnose,以及 DSH、Kimi Code、ZCode 的 doctor)新增展示 matched_key。
  • 服务端 HTTP/MCP API、OpenAPI Schema 与持久化数据库表结构均无破坏性变更。

测试与验证情况

  • 全仓代码质量、能力清单、指引 Skill、生命周期副本同步、Ruff 与 ty 类型检查(本机 macOS arm64 实测;macOS fcntl.F_GETPATH 的 APFS 大小写与 NFD/NFC 一致性已在 macOS 本机验证,CI 在脱离 Draft 后自动覆盖 Linux 与 windows-latest):
    • make check(All checks passed!,62 passed)
    • make contract-test(69 passed)
    • make harness-lint(All checks passed!)
  • Python 单元测试、插件契约、CLI 与真实 Server 端到端测试:
    • uv run python -m pytest -n auto tests/test_git_worktree.py tests/test_cli_scope.py tests/test_session_import.py tests/claude_code_plugin tests/codex_plugin tests/workbuddy_plugin tests/integrations tests/test_integration_manifest.py tests/test_cli_zcode.py tests/test_cli_workbuddy.py tests/e2e/test_shared_workspace_scope_binding.py tests/e2e/test_codex_service_chain.py tests/e2e/test_zcode_service_chain.py tests/e2e/test_bub_service_chain.py -q(1197 passed, 11 skipped)
  • JavaScript 与 TypeScript 宿主单测、端到端、类型检查与构建产物校验:
    • pnpm --dir integrations/dsh/plugins/powercontext test(31 files, 1209 passed)及 node --test integrations/dsh/plugins/dsh-pet/tests/adapter.test.mjs integrations/dsh/plugins/dsh-pet/tests/package.test.mjs(5 passed)
    • pnpm --dir integrations/opencode/plugins/powercontext test && pnpm --dir integrations/opencode/plugins/powercontext run typecheck(11 files, 175 passed)
    • pnpm --dir integrations/pi/plugins/powercontext test && pnpm --dir integrations/pi/plugins/powercontext run typecheck(18 files, 232 passed)
    • node --test integrations/zcode/plugins/powercontext/tests/*.test.mjs(116 passed, 1 skipped, 0 failed)
    • node --test --test-name-pattern="workspace|scope|doctor|worktree|binding|unbind" integrations/kimi-code/plugins/powercontext/tests/plugin.test.mjs(8 passed, 0 failed)

AI 使用声明

本 PR 在 Antigravity(Gemini)与 Claude Code 辅助下完成实现、审查与验证。

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant