Skip to content

fix(workspacedeps): 依赖目录按来源全局存储一份,读取依赖定义不再写库 - #1747

Draft
HoneyBBQ wants to merge 2 commits into
mainfrom
fix/dependency-catalog-global
Draft

HoneyBBQ wants to merge 2 commits into
mainfrom
fix/dependency-catalog-global

Conversation

@HoneyBBQ

@HoneyBBQ HoneyBBQ commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Author

  • Human
  • Agent

Type

  • bug
  • feat
  • test

Summary

问题

依赖定义和依赖目录按团队存储,开启了 FORCE RLS。它们的内容来自 server 级配置的 Supermarket 地址,读取时都会按 sha256 校验,和团队无关。

这样存有三个后果:

  • 同一个版本,每个团队各存一份、各下载一次。
  • 读取依赖定义的查询是 UPDATE ... SET last_accessed_at = now() ... RETURNING,每读一次就写一次库、提交一次。
  • 内存快照只存一个团队的目录,按团队交替读取时必定不命中。ReapStale 每次又先加载整份目录,再查有没有未完成的操作。

后台任务按团队执行时,写入次数等于团队数 × 目录条目数 × 执行频率,写入不会停。

改动

  • 新迁移 0173 在 template schema 下建 workspace_dependency_definitions 和 workspace_dependency_catalogs 两张全局表,不带 team_id,也不开 RLS。做法与 provider 模板表相同。
  • 迁移只复制 release 和安装包两个哈希都校验通过的定义,并去重。
  • 目录每个来源合并成一份:最新那份目录的现行条目作为当前条目,其他团队见过的条目标为已下架,保留版本。没有有效定义的条目丢弃。
  • 迁移 0174 删除两张按团队的旧表。删除前先把旧表里已校验、全局表还没有的定义补拷过去,覆盖上一版本在发布窗口内取到的新定义。两步分成两个迁移,需要滚动发布的部署可以先只执行 0173,旧版本下线后再执行 0174。
  • 依赖定义改为只增不删,去掉 GC 和 last_accessed_at。
    • 原规则要判断「没有任何团队装着它」才删,表改成全局后这需要跨团队读安装记录。
    • 回滚可能用到的版本有一部分只记在工作区文件里,数据库枚举不全。
    • 新版本只在依赖的脚本或清单改动时出现,一份约几 KB。
  • 读取依赖定义改为普通 SELECT。
  • 内存快照和读取时触发的重新校验按来源存,不再按团队。
  • ReapStale 先查未完成的操作,没有需要处理的就直接返回,不加载目录。
  • 更新检查读已存的目录,不再强制刷新。后台刷新会让目录保持在一个刷新周期以内。

Related Issues

Closes #1744

Validation

  • go build ./...、go vet 通过。./internal/workspacedeps/...、./internal/db/... 的单元测试和集成测试通过,包括 TestTeamSchemaGuard、TestRLSPolicyGuard、team_id 回填、复合键和 provider 模板全局性的守卫测试。mise run lint:go 通过。
  • 迁移集成测试覆盖以下情况:
    • 多个团队存有重复定义。
    • 一条定义的哈希不符。
    • 各团队的目录不同,有的版本不同,有的下架集合不同。
    • 迁移后检查全局表的行、合并后的目录 JSON,以及旧表恢复 FORCE RLS。
    • down 后再 up。
  • 0174 的迁移集成测试:在 0173 时只往旧表写一条校验通过的定义和一条哈希不符的定义,升到 0174 后前者进入全局表、后者没有,两张旧表已删除;降回 0173 时旧表恢复且开启 FORCE RLS,再次升级。
  • 新增测试:
    • 读取定义不会改动行。
    • 两个团队上下文读同一来源时复用内存快照。
    • 没有未完成操作时 ReapStale 不加载目录。
  • dev 环境(devenv compose,提交 7e8637303,未含 0174):
    • 迁移到 0173,两张全局表存在。
    • 定义 33 行,目录 1 行。
    • 应用市场和 Bot「应用」页的名称、图标正常显示。
    • 重载页面 6 次并等待 150 秒,定义表的 UPDATE 计数保持 0。目录表只在每个刷新周期更新一次。
  • 未覆盖:从界面安装一个新依赖直到完成。

Screenshots / Recordings

  • 应用市场:依赖目录的名称和图标都从全局表读取。

    应用市场

  • Bot「应用」页:已安装的依赖正常列出。

    Bot 应用页

Human QA

  • Human QA passed

Dependency definitions and the cached catalog come from the server-level
Supermarket source and are verified by digest on every read, but they were
stored per team under FORCE RLS. Every team downloaded and stored its own
copy, every definition read ran an UPDATE to renew a retention lease, and
the in-memory snapshot missed whenever reads alternated between teams.

Migration 0173 adds template.workspace_dependency_definitions and
template.workspace_dependency_catalogs without team_id or RLS and copies
the rows whose release and archive digests verify, deduplicated, plus one
merged catalog per source. The per-team public tables stay for Servers
running the previous release during a rolling deploy.

Definitions are now append-only: the lease column, the prune query and
its call are removed, and a definition read is a plain SELECT. The
snapshot cache and read revalidation are keyed by source only. ReapStale
loads the catalog only when an unlocked operation is left in progress,
and the update worker reads the stored catalog instead of forcing a
registry refresh.

Refs #1744
@github-actions github-actions Bot added bug Fixes a bug or unexpected behavior change:migrations Adds, modifies, or removes database migrations change:server Changes backend code, configuration, or API contracts size:M PR size uses the larger of added or deleted lines, excluding generated files labels Nov 5, 2026
Migration 0174 copies verified definitions that Servers still on the previous
release wrote into public.workspace_dependency_definitions into the global
template table, then drops the per-team definition and catalog tables. The
catalog is rebuilt by the refresher. 0001_init reflects the final state.

Must be deployed only after 0173 and after no previous-release Server runs.

Refs #1744
@HoneyBBQ
HoneyBBQ force-pushed the fix/dependency-catalog-global branch from 62f7c3c to 33283ec Compare November 5, 2026 07:14

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

bug Fixes a bug or unexpected behavior change:migrations Adds, modifies, or removes database migrations change:server Changes backend code, configuration, or API contracts size:M PR size uses the larger of added or deleted lines, excluding generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 依赖定义按团队重复存储,读取依赖定义时写库

1 participant