Skip to content

fix(web,gateway): 修复工作会话切换竞态并收敛文件预览工作区边界#653

Merged
minorcell merged 10 commits into
1024XEngineer:mainfrom
wynxing:main
May 18, 2026
Merged

fix(web,gateway): 修复工作会话切换竞态并收敛文件预览工作区边界#653
minorcell merged 10 commits into
1024XEngineer:mainfrom
wynxing:main

Conversation

@wynxing
Copy link
Copy Markdown
Collaborator

@wynxing wynxing commented May 17, 2026

背景

本 PR 解决 Web 工作会话/工作区切换中的竞态覆盖问题,以及网关文件预览接口可能受客户端 workdir 影响导致的边界风险。

改动概览

1) 前端切换一致性修复

  • web/src/stores/useSessionStore.ts

    • 调整 switchSession 顺序:先 clearMessages(),再 setTransitioning(true),确保切换窗口期事件过滤有效。
    • fetchSessions 增加请求代际序号,晚到旧响应不再覆盖新状态。
    • resetForWorkspaceSwitch 会使旧请求失效。
  • web/src/stores/useWorkspaceStore.ts

    • switchWorkspace / createWorkspace 增加 switchSeq,仅最新切换可落状态。
    • 旧切换晚到响应直接丢弃,避免状态回跳。
  • web/src/utils/eventBridge.ts

    • 增加统一会话过滤:非全局关键事件需匹配 currentSessionId,否则丢弃。
    • 保留全局关键事件通路(如 Error,以及会话对齐所需事件)。

2) 网关工作区边界收敛

  • internal/cli/gateway_runtime_bridge.go
    • resolveListFilesRoot 不再信任客户端 input.Workdir
    • 根目录解析统一为:当前工作区根 + 合法会话 workdir(需在 workspace root 内)。
    • 会话 workdir 越界时返回受控错误。
    • ListFiles / ReadFile / ListGitDiffFiles / ReadGitDiffFile 统一复用该策略。

3) 文档更新

  • docs/reference/tui-gateway-contract-matrix.md
    • 明确文件预览相关接口受当前工作区边界约束,workdir 字段保留兼容但不作为越界覆盖入口。

测试

已新增/更新测试

  • 后端:
    • internal/cli/gateway_runtime_bridge_test.go
    • 覆盖:
      • 忽略外部 workdir
      • 会话 workdir 越界拒绝
      • git diff 预览接口同策略
  • 前端:
    • web/src/stores/useSessionStore.test.ts
    • web/src/stores/useWorkspaceStore.test.ts
    • web/src/utils/eventBridge.test.ts
    • 覆盖:
      • 切换窗口 isTransitioning 保持
      • 并发切换晚到响应不回写
      • 旧会话事件被过滤

本地验证

  • go test ./internal/cli -run "ResolveListFilesRoot|ListFiles|ReadFile|GitDiff"
  • npm test -- --run src/stores/useSessionStore.test.ts src/stores/useWorkspaceStore.test.ts src/utils/eventBridge.test.ts

兼容性说明

  • JSON-RPC 字段兼容保持不变;
  • 语义调整为“当前工作区边界优先”,客户端 workdir 不再作为文件预览根目录覆盖来源。

风险与回滚

  • 风险:依赖旧行为(请求级 workdir 覆盖)的调用方将感知更严格边界。
  • 回滚:可先回滚前端代际逻辑;边界收敛建议保持(安全基线)。

close #652

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 17, 2026

Codecov Report

❌ Patch coverage is 73.68421% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/cli/gateway_runtime_bridge.go 73.68% 5 Missing and 5 partials ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 2 issues that can still leak stale state across session/workspace switches.

Comment thread web/src/stores/useSessionStore.ts
Comment thread web/src/stores/useWorkspaceStore.ts Outdated
@wynxing
Copy link
Copy Markdown
Collaborator Author

wynxing commented May 17, 2026

@xgopilot 补充测试,提高CI测试的覆盖率到85以上,并且把更改提交个PR到我的fork仓库

@fennoai
Copy link
Copy Markdown

fennoai Bot commented May 17, 2026

完成情况

已补充 internal/cli/gateway_runtime_bridge_test.go 的定向测试,覆盖了这次边界收敛里之前遗漏的几个分支:

  • 会话 workdir 为空时回退到工作区根
  • 会话加载出现非 not found 异常时直接返回错误
  • 工作区根解析的配置路径 / cwd 回退路径
  • isPathWithinRoot 对根目录、子目录、越界目录和符号链接越界的判断
  • loadStoredSession 在 session store 不可用或不支持加载时的错误分支

本地验证已完成:

  • go test ./internal/cli -run 'ResolveListFilesRoot|ResolveWorkspaceRootForFileAccess|LoadStoredSession|IsPathWithinRoot|ListFiles|ReadFile|GitDiff'
  • cd web && npm test -- --run src/stores/useSessionStore.test.ts src/stores/useWorkspaceStore.test.ts src/utils/eventBridge.test.ts src/components/layout/Sidebar.test.tsx

补充说明:我本地按 gateway_runtime_bridge.go 本次新增可执行变更行估算,覆盖约为 86.84%;未覆盖部分主要是几个系统级错误返回分支,正常测试路径下基本不可达。没有跑 go test ./... 全量回归。

已提交并推送到当前分支:af9fcb33 test(cli): cover file preview workspace boundary helpers

Fork PR

已同步推到你的 fork 分支,并创建了 fork 内 PR:

View job run

xgopilot and others added 2 commits May 17, 2026 10:29
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: Yumiue <188874804+Yumiue@users.noreply.github.com>
test(cli): improve file preview boundary coverage
@wynxing
Copy link
Copy Markdown
Collaborator Author

wynxing commented May 17, 2026

@xgopilot 补充测试,提高CI测试的覆盖率到85以上,并且把更改提交个PR到我的fork仓库

@fennoai
Copy link
Copy Markdown

fennoai Bot commented May 17, 2026

已完成

已补充并推送以下测试:

  • internal/cli/web_command_test.go
    • 覆盖静态目录校验、前端构建陈旧判断、buildFrontendreadGatewayToken、浏览器打开链路、监听地址回退、runWebCommand 的错误/回退分支。
  • web/src/stores/useWorkspaceStore.test.ts
    • 补齐 renameWorkspace 成功/失败、deleteWorkspace 失败分支。
  • web/src/components/layout/Sidebar.test.tsx
    • 补齐工作区切换按钮在当前/非当前工作区下的行为分支。

本地验证:

  • go test ./... 通过
  • npm test -- --run src/stores/useSessionStore.test.ts src/stores/useWorkspaceStore.test.ts src/utils/eventBridge.test.ts src/components/layout/Sidebar.test.tsx 通过(122 tests)
  • 前端相关覆盖率复核:useSessionStore.ts 86.99%useWorkspaceStore.ts 90.38%
  • 全量 Go 覆盖率本地复核结果为 82.9%;仓库内 CI 未显式配置 85% 阈值,因此无法在本地直接复现你提到的具体 CI 判定口径。

代码已推送到当前 PR 分支:fork-pr-653-1779017134
Fork 仓库附加 PR:wynxing#30

View job run

xgopilot and others added 2 commits May 17, 2026 11:36
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: Yumiue <188874804+Yumiue@users.noreply.github.com>
test(cli,web): raise coverage for workspace switch paths
@minorcell minorcell merged commit ffa04d1 into 1024XEngineer:main May 18, 2026
2 of 3 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.

RFC Proposal: Web 工作会话切换与文件预览边界一致性治理

3 participants