From 36b9cb2bc0c84d9da3fb894a0e0d9ca99b6e5b95 Mon Sep 17 00:00:00 2001 From: qyh15 Date: Sat, 7 Nov 2026 17:29:57 +0800 Subject: [PATCH] fix(folders): make the folder overlay crash-safe and consistent The folder sidecar was written as a whole document with no concurrency control, and any read failure silently degraded to an empty document, so a later edit could erase every workspace's folder configuration. Rework the read/write path end to end: - Agent side: expose a content revision, require If-Match on PUT, answer 409 with the current document, serialise read-compare-write, and fail the GET loudly instead of serving an empty overlay. Unknown schema versions are reported and never downgraded back to v1. - Client: load/save report failure instead of fabricating an empty document, edits go through one serial queue with optimistic update, rollback on error, and reload on conflict. Editing is blocked while the overlay cannot be read. Creating a workspace is compensated when the follow-up save fails, so no orphan workspace is left behind. - Paths: normalise NFC and lexically fold "." and "..", decide absoluteness by spelling instead of process.platform, share one implementation between core and host, and keep an existing root when a member is re-registered at the same path. - Remote folders: resolve the owner root from the SSH probe instead of the "~" alias, and make "subfolder of this project" versus "register a new cloud workspace" an explicit choice in the dialog. - Stop shipping the client source map, which embedded the full TypeScript sources in the published package. - Track the SSH integration plan and cover the new behaviour with tests (CAS, persistence, remote path parsing). Verification: both tsconfigs typecheck clean, 288 tests pass, build succeeds, and all three ad-hoc verifiers pass. --- .gitignore | 12 +- CHANGELOG.md | 51 ++ DEVELOPMENT.md | 23 +- docs/remote-ssh-integration-plan.md | 350 +++++++++++ lib/client.js | 816 +++++++++++++++++++------- lib/client.js.map | 1 - lib/index.js | 458 ++++++++++++--- lib/types/client/FoldersDialog.d.ts | 9 +- lib/types/client/folders-persist.d.ts | 80 ++- lib/types/client/locales.d.ts | 2 +- lib/types/client/remote-paths.d.ts | 18 + lib/types/core/default-workspace.d.ts | 30 +- lib/types/core/folders.d.ts | 58 +- lib/types/host-folders.d.ts | 93 ++- src/client/FoldersDialog.tsx | 88 ++- src/client/GroupsBrowser.tsx | 296 +++++++--- src/client/folders-persist.ts | 193 ++++-- src/client/locales.ts | 24 + src/client/remote-paths.ts | 36 ++ src/core/default-workspace.ts | 43 +- src/core/folders.ts | 137 ++++- src/host-folders.ts | 347 +++++++++-- src/index.ts | 138 ++++- tests/folders-cas.test.ts | 296 ++++++++++ tests/folders-persist.test.ts | 129 ++++ tests/folders.test.ts | 13 +- tests/remote-paths.test.ts | 50 ++ tools/verify-folders.mjs | 6 +- tsdown.config.ts | 5 +- 29 files changed, 3137 insertions(+), 665 deletions(-) create mode 100644 docs/remote-ssh-integration-plan.md delete mode 100644 lib/client.js.map create mode 100644 lib/types/client/remote-paths.d.ts create mode 100644 src/client/remote-paths.ts create mode 100644 tests/folders-cas.test.ts create mode 100644 tests/folders-persist.test.ts create mode 100644 tests/remote-paths.test.ts diff --git a/.gitignore b/.gitignore index a020bfa..0a5d140 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,14 @@ node_modules/ *.log .DS_Store -# 开发工程文件:docs 五级文档框架不进项目 git 仓库 -docs/ +# Build / test byproducts. The published client bundle must NOT ship a +# sourcemap: tsdown now builds with sourcemap:false (see tsdown.config.ts). +*.map +*.tsbuildinfo +dist/ +coverage/ +tools/_tmp/ + +# Local-only agent instructions. Project design docs under docs/ ARE tracked: +# remote-ssh-integration-plan.md documents the two open task-board cards. AGENTS.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 98e9f35..ed2163f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,56 @@ # 更新日志 +## 未发布 — 文件夹管理安全性与一致性修复(2026-11-07) + +针对「工作区内的文件夹管理」扫描发现的 10 类问题做的一次集中修复。本次只改本插件, +未触碰核心 workspace.json / session 存储;`lib/` 已重新构建,Source map 不再随包发布。 + +### 数据完整性(高危) +- 读取 `workspace-groups.folders.json` 失败(EACCES / EISDIR / JSON 损坏)现在返回 HTTP 500, + 不再伪装成「空文档」;客户端 loadFolders 改为 `ok:false` 并禁用全部编辑,杜绝「读失败 → 下次 PUT + 写回空文档 → 所有工作区文件夹永久清空」。 +- 新增基于文件内容的 `revision`(sha256 前 16 位)+ `ETag` / `If-Match` 乐观并发:PUT 不匹配返回 + 409 并携带服务端最新文档;宿主侧 read→compare→write 串行化,客户端写操作排队、失败回滚、 + 409 自动重载。多窗口不再静默互相覆盖。 +- 未知 schema 版本只报告 `schemaWarning` 并禁用编辑,绝不降级写回 v1。 +- PUT body 上限从 64KB 提到 1MB(合法文档上限约 92KB 曾永远 413);保存请求去掉 `keepalive` + 并加 15 秒超时,避免大文档被静默截断。 + +### 路径与远程判定 +- `canonicalPath` 增加 NFC 归一与词法 `.` / `..` 折叠;`isAbsoluteFolderPath` 改为按路径拼写判定, + 宿主与客户端共用同一函数,不再因 `process.platform` 漂移而丢弃跨平台 sidecar 条目。 +- `defaultRemoteMirrorBase` 硬编码 `~/.dsh/remote-workspaces` 的问题改为显式诊断: + `$DSH_HOME` 与硬编码根不一致时 GET 返回 `remoteBaseWarning` 并在界面显示。 +- 宿主写校验现在强制 `kind:'remote'` 的 member.path 必须位于镜像根内,并拒绝 `..` 段; + 镜像归属不再只靠浏览器自觉。 +- `subfolderOf` 用浏览器返回的绝对 `resolved` 路径比较(`~` 别名由 `openFoldersFor` 先解析), + 分隔符归一;云端输入框改为显式「本项目子目录 / 独立云端工作区」二选一,消除一个输入框两种语义。 + +### 会话与孤儿工作区 +- 创建会话前必须等文件夹 overlay 落盘成功;原生工作区创建失败可补偿注销,远程文件夹创建有 + pending 复用,避免重复建镜像。 +- `addMember` 同路径重注册时保留 `root`;`isDefaultWorkspace` 改为路径判据优先; + 跨 entry 拒绝「member.workspaceId 同时是 owner」,宽容读会丢弃这类旧数据。 +- `parseFolderEntry` 允许缺 `folders` 字段(不再连带丢掉 members),`remoteSubpath` 与 + `joinSubpath` 对绝对/盘符/UNC 输入的判定统一;`joinSubpath` 额外拒绝 `C:foo`。 + +### 测试与工程 +- 新增 `tests/folders-cas.test.ts`、`tests/folders-persist.test.ts`、`tests/remote-paths.test.ts` + 覆盖上述回归点;`tsc -p tsconfig.json` / `tsconfig.client.json` 与 288 个 vitest 用例全部通过。 +- `tsdown.config.ts` 关闭客户端 sourcemap,`lib/client.js.map`(内嵌完整源码)已从构建产物移除; + `.gitignore` 补 `*.map` / `*.tsbuildinfo` / `dist/` / `coverage/` / `tools/_tmp/`,并取消对 + `docs/` 的忽略,把 `remote-ssh-integration-plan.md` 纳入版本控制。 +- 工作区根目录新增 README 说明目录职责;`dsh-workspace-groups-0.4.2/` 与 `tools/` 各加说明, + 标明 0.4.2 是**有意保留的重建基线,不可删除**。未删除/移动任何既有目录或文件。 + +### 仍未处理(需另行确认) +- `lib/` 里历史遗留的其它 37 个 tracked 文件是构建产物,未做 `git rm --cached`;若要改为 + 构建时再生成,需要同步调整发布流程。 +- `iggfeed.yaml` 与 `tools/fetch_clash_sub.py` 含本机代理地址/订阅 URL,仅在工作区 README 中提示, + 未改动。 +- remote-ssh 2.4.40 的远程子目录会话限制、SSH 能力完整内置、会话拆分仍是 + `docs/remote-ssh-integration-plan.md` 与任务板里的独立待办,本次未动。 + ## 1.0.0 — 2026-10-28 首个本地稳定版本,包名 `workspace-groups-local`。基于原项目扩展,保留原 MIT 许可证与版权声明。 diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 8927a97..28d5d3e 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -162,12 +162,25 @@ $installed = "C:\Users\qyh15\.dsh\profiles\desktop\node_modules\@dsh-xhl\workspa ## 6. 已知坑 -- **沙箱内跑不了 vitest**:`spawn` 被 DSH 沙箱拦(EPERM),在普通 PowerShell 终端里跑。 +- **沙箱内跑不了 vitest**:`spawn` 被 DSH 沙箱拦(EPERM);需 `danger-full-access` + 或在普通 PowerShell 终端里跑。pnpm 的 hoisted 布局偶尔缺 `@vitest/*` 顶层链接, + 这时用 `node_modules/.pnpm/vitest@*/node_modules/vitest/vitest.mjs` 加一个不 import + `vitest/config` 的纯 JS 配置即可绕过。 - **`node_modules/.bin/*.CMD` 直调会挂**:pnpm 里嵌套的 pnpm 脚本在 DSH 沙箱下会 - `Could not determine executable`,统一用 `node