Skip to content

fix(v2): bound alternates at any depth and walk nested Git directories once - #438

Open
wanglongan587 wants to merge 4 commits into
ora-space:mainfrom
wanglongan587:fix/1396-nested-git-digest-lows
Open

wanglongan587 wants to merge 4 commits into
ora-space:mainfrom
wanglongan587:fix/1396-nested-git-digest-lows

Conversation

@wanglongan587

@wanglongan587 wanglongan587 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Refs ora-space/circus#1396
Parent: ora-space/circus#1395

Summary

Follows up cloud#435 (fe0b378b) on the five Low items from the owner review in ora-space/circus#1387. Default reclaim is unchanged; the only behavior that changes is the deeper alternates case, which now fails closed.

  • v2-server/internal/workspacefs/reclaim_check.go
    • objects/info/alternates anywhere inside a nested Git directory now makes the digest incomplete, not only at its top. A deinitialized submodule keeps .git/modules/<name>/objects/info/alternates, which can point the object store outside the checkout where the revision cannot see it change; the inspection missed that and called the directory discardable.
    • That check is limited to a file inside a Git directory: the path before objects/info/alternates must itself lie inside one of the Git directories. git init --separate-git-dir cache/objects/info/alternates puts the metadata where an alternates file would sit, and that repository stays complete.
    • The strict second pass over a repository's own .git is gone: its Git directories are resolved before the walk and checked in the same pass. It remains true that a Git directory falling inside another repository's already-digested subtree is checked separately by checkGitDir, and that mutually nested repositories walk their overlap once per repository, so the comments claim no more than that.
    • GitIdentityComplete's comment now says what actually makes the digest incomplete instead of only naming the size limit.
  • v2-server/internal/workspacefs/reclaim_check_test.go: four new subtests in TestInspectReclaimMarksSymlinksAndAlternatesInNestedGitIncomplete.
    • alternates deeper inside a nested Git directory — red before this change (GitIdentityComplete = true, want false); the alternates path goes five levels up from the submodule's objects directory and the fixture asserts the shared store resolves.
    • Git directory named like an alternates file stays complete — red without the boundary check above (GitIdentityComplete = false, want true).
    • nested Git directory resolving to the checkout root and alternates inside .git/modules entry — pin two checks that had no coverage; they pass on fe0b378b too.
  • v2-server/internal/workspacereclaim/reclaim_test.go: alternates behind a deinitialized submodule in TestDiscardIsRefusedWhenNestedGitUsesSymlinksOrAlternates — red before (preview = {... Discardable:true}, so the discard would have run); now the preview is undelivered/git_state without discard and discard returns ErrDiscardUnverifiable with the conversation and wt intact. The alternates path is the same five levels up and its target is asserted.
  • v2-server/internal/workspacefs/README.md, v2-server/README.md: name the commondir inside .git/modules/<name> and a Git directory resolving to the checkout root, state that alternates at any depth inside a Git directory are checked (a Git directory named like one is not), say only that the subtree's own second pass is gone, and list the legitimate repositories the boundary rejects as known limitations — hooks kept as symlinks, clone --shared/--reference, submodule alternateLocation, a multi-worktree submodule sharing a commondir.

The merged cloud#435 description also names its circus parent now (#1354).

ADR status: decisions/cloud/project-git/20261119-reclaim-deleted-conversation-workspace.md stays implemented; no new decision. D2's rule already says a nested Git directory holding a symlink or using objects/info/alternates makes the digest incomplete, so checking it at any depth is the same promise applied to the submodule case. The evidence for the deeper case is registered with the next landing sync of that ADR and its core cases (tracked in ora-space/circus#1395), since test-cases/cloud/project-git/workspace-reclaim.md is pinned to merged code.

Review follow-up (cbef66b → d8965570)

  1. Fixtures point at a real shared object store (ondesavoir 1, zhangxinhao P2-3): both deep-alternates fixtures used four .., which lands in wt/store.git/objects; they now use five and assert the target resolves before the digest runs.
  2. Single-walk and entry-budget wording narrowed (ondesavoir 2, zhangxinhao P2-1): the baseline checkGitDir counted with its own local checked, so the old second pass did not double-charge maxNestedEntries; the PR description and both READMEs now only claim the removed subtree pass. The suggested repoTree comment is adopted.
  3. alternates matching limited to real alternates files (zhangxinhao P2-2): applied in walkTree and checkGitDir, with the --separate-git-dir regression above.

Verification

WSL Ubuntu, non-root wanglongan (uid 1000), Go 1.27.1, PostgreSQL 18 with a dedicated ora_1396_test database:

  • gofmt -l ./internal/workspacefs ./internal/workspacereclaim — empty.
  • go vet ./..., go build -buildvcs=false ./... — pass.
  • TEST_DATABASE_URL=… go test -race -count=1 ./internal/workspacefs ./internal/workspacereclaim ./internal/gitexclude ./internal/store ./internal/chat ./internal/boot ./internal/agentruntime ./internal/server — ok (7.9s / 24.4s / 1.2s / 74.9s / 5.3s / 1.0s / 6.3s / 101.3s).
  • Red/green: with reclaim_check.go at fe0b378b, alternates deeper inside a nested Git directory fails with GitIdentityComplete = true, want false, and alternates behind a deinitialized submodule fails with Discardable:true; with the boundary check relaxed to a bare suffix match, Git directory named like an alternates file stays complete fails with GitIdentityComplete = false, want true. All 13 inspection subtests and 5 discard subtests pass at this head.
  • git diff --check clean; the test database is dropped afterwards.

…s once

The digest rejected objects/info/alternates only at the top of a nested Git
directory, so the store a deinitialized submodule under .git/modules points at
could change without the revision noticing. Any depth now marks the digest
incomplete.

Each nested repository is also walked once: its Git directories are resolved
first and checked strictly in the same traversal, so the entry budget counts
every entry once instead of twice.

Cover the alternates check inside .git/modules, a nested Git directory that
resolves to the checkout root, and a deep alternates file behind a
deinitialized submodule, in both the inspection and the discard paths.
@wanglongan587
wanglongan587 force-pushed the fix/1396-nested-git-digest-lows branch from 7b0946f to 89e592d Compare November 23, 2026 06:35
Name the commondir inside .git/modules and a Git directory resolving to the
checkout root as incomplete, and list the legitimate repositories the boundary
rejects so a discard has to go through operations: hooks kept as symlinks,
clone --shared/--reference, submodule alternateLocation, and a multi-worktree
submodule sharing a commondir.
@wanglongan587
wanglongan587 force-pushed the fix/1396-nested-git-digest-lows branch from 89e592d to cbef66b Compare November 23, 2026 06:40

@zhangxinhao zhangxinhao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM(cbef66b)

@wanglongan587 我看了这个 PR,理解它是要把嵌套 Git 目录任意深度的 objects/info/alternates 纳入丢弃前检查,并把仓库子树内的 Git 目录校验合并到摘要遍历中。可以合并。相关竞态测试通过,包括 PostgreSQL 上的丢弃用例。建议修正 alternates 匹配对合法 Git 目录的误判,并收窄文档中的遍历和计数承诺。

  • 🔧 3 处小改已写成建议(P2-1、P2-2、P2-3):见行内 suggestion,可以一键采纳,也可以不改或留到以后。
  • 测试:git diff --check fe0b378b3a1f35cffa769f6499e7ddcf29506705 HEAD 通过;gofmt -l v2-server/internal/workspacefs/reclaim_check.go v2-server/internal/workspacefs/reclaim_check_test.go v2-server/internal/workspacereclaim/reclaim_test.go 通过;go vet ./internal/workspacefs ./internal/workspacereclaim ./internal/gitexclude 通过;go test -race -count=1 ./internal/workspacefs ./internal/gitexclude 通过;go test -race -count=1 ./internal/workspacereclaim -run 'Test(Seal|DiscardIsRefusedWhenNestedGitUsesSymlinksOrAlternates|DiscardStillAllowsWorkingTreeSymlinksInNestedRepository|DiscardIsStaleWhenARepositoryBehindAPointerChanges|DiscardIsStaleWhenASharedRepositoryNamedByCommondirChanges)' 通过;没跑全量和 e2e。

涉及删除数据,建议 owner 再看一眼。

以上都是建议,怎么处理由你决定。

自动检视 · 基于 cbef66b · 第 1 轮(全量)

d.walkTree(r, dir, true, "")
// repoTree writes a nested repository: its working tree by content and links by target, and the
// Git directories inside it (gitDirs, relative to the checkout root) strictly, all in one pass.
// One walk binds the whole repository, so the entry budget counts each entry once.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2-1(建议) 这次消除了单个仓库子树内的 .git 复扫,但如果嵌套仓库本身还含有已检出的子模块,gitIdentity 会先遍历父仓库整棵子树,再对作为独立 g.Repositories 条目的子模块调用 repoTree,重叠条目仍会重复占用预算。另外,merge-base 的 checkGitDir 用独立的 checked 计数,同一仓库的第二遍检查原本就不会累加到 d.entries。建议同步收窄注释、两处 README 和 PR 描述,只承诺消除单个子树内 Git 元数据的校验复扫。

Suggested change
// One walk binds the whole repository, so the entry budget counts each entry once.
// Git metadata inside this subtree is validated during the same walk that hashes it.

可直接采纳,也可以不改。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已处理,谢谢。

}
if alternates != "" && p == alternates {
inGit := strict != nil && within(p, strict)
if inGit && strings.HasSuffix(p, "/objects/info/alternates") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2-2(建议) 这里按 checkout 相对路径匹配后缀,Git 目录自身的路径也可能命中。我用 git init --separate-git-dir 将元数据放在 cache/objects/info/alternates,git fsck --full 通过,merge-base 上摘要完整,但当前 head 会把目录本身当作 alternates,拒绝丢弃。建议确认整个 objects/info/alternates 路径都位于受检查的 Git 目录内,并在 checkGitDir 的后缀检查中应用相同的边界判断。

Suggested change
if inGit && strings.HasSuffix(p, "/objects/info/alternates") {
if parent, ok := strings.CutSuffix(p, "/objects/info/alternates"); ok && within(parent, strict) {

可直接采纳,也可以不改。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已处理,谢谢。

if err := os.RemoveAll(filepath.Join(wt, "mod")); err != nil {
t.Fatal(err)
}
deliveryWrite(t, wt, ".git/modules/mod/objects/info/alternates", "../../../../store.git/objects\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2-3(建议) Git 从对象库目录 wt/.git/modules/mod/objects 解析这个相对路径。四个 .. 回到 wt,所以用例实际指向不存在的 wt/store.git/objects,只验证了碰到 alternates 文件就拒绝。建议改成五个 ..,并同步修正 v2-server/internal/workspacereclaim/reclaim_test.go:1493,让两处用例都覆盖有效的共享对象库。修正路径后,两处用例均通过。

Suggested change
deliveryWrite(t, wt, ".git/modules/mod/objects/info/alternates", "../../../../store.git/objects\n")
deliveryWrite(t, wt, ".git/modules/mod/objects/info/alternates", "../../../../../store.git/objects\n")

可直接采纳,也可以不改。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

已处理,谢谢。

@ondesavoir ondesavoir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

请修正测试夹具和单次遍历的说明后重送审。审查基于 cbef66b6219bb47435683f95f86d14f4c4e3217d。

  1. 让深层 alternates 用例引用实际存在的对象库。 Git 相对 objects 目录解析 alternates。两个新增的深层用例都从 wt/.git/modules/mod/objects 用四个 ..,实际落在 wt/store.git/objects,而对象库建在 checkout 根下。当前断言只能证明发现该文件就拒绝丢弃。请改成指向真实对象库的路径,并在检查前验证目标可解析,确保测试覆盖已 deinit 子模块仍引用共享对象的场景。顶层 .git/modules/mod 用例的四层路径是正确的。检查层夹具 · 回收层夹具
  2. 收窄单次遍历和条目预算的说明。 基线 fe0b378b 的 checkGitDir 用局部 checked,第二遍扫描没有再次扣减 d.entries,因此 PR 描述所称的旧版上限误判没有这条依据。当前实现确实移除了普通 repo/.git 的第二遍扫描,但如果仓库的 Git 目录落在先前摘要过的另一仓库工作树里,仍会通过 checkGitDir 再遍历。两个仓库目录本身有包含关系时,无条件调用 repoTree 也仍会重复计入重叠子树。请把两份 README 和 PR 描述限定在本次确实消除的扫描范围,或者补齐跨仓库去重及其回归测试。摘要遍历

已核对 cloud#435 的 Parent 和 specs main@df1f362e 中 ADR 的 implemented 状态。D2 要求摘要绑定交付外的 Git 内容,深层 alternates 的保守处理与此一致。

…irectory

Matching the suffix alone also caught a Git directory that is itself named
objects/info/alternates, which `git init --separate-git-dir cache/objects/info/
alternates` creates: such a repository is legitimate and stays complete now,
while a file of that name at any depth inside a Git directory still makes the
digest incomplete.

The two deep-alternates fixtures pointed four levels up from the submodule's
objects directory, which lands in `wt/store.git/objects` instead of the shared
store at the checkout root; they now go five levels up and assert the target
resolves, so the case covers a real shared object store. Add the
--separate-git-dir regression beside them.
The second pass over a repository's own .git no longer runs, but a Git
directory inside another repository's already-digested subtree is still
checked separately, and mutually nested repositories still walk their overlap
once per repository. Say that instead of claiming every entry is counted once.

@ondesavoir ondesavoir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

复审通过。两处深层 alternates 夹具现引用实际存在的共享对象库。PR 描述和两份 README 已将遍历改动限定为单个仓库子树内的 Git 元数据复扫。新增的路径边界判断覆盖了同名 Git 目录,并保留对目录内深层 alternates 的检查。核对了 specs main@df1f362e 中已 implemented 的 ADR,变更符合 D2 的内容摘要要求。

@zhangxinhao zhangxinhao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM(d896557)

@wanglongan587 我看了这个 PR,理解它是要把嵌套 Git 目录任意深度的 objects/info/alternates 纳入丢弃前检查,并在计算仓库子树摘要时一并校验其中的 Git 元数据。这是第 2 轮,我看了 cbef66b 之后的改动。整体没问题,可以合并。

  • ✅ 上轮的 P2-1、P2-2、P2-3 已处理,谢谢。
  • 测试:git diff --check fe0b378b3a1f35cffa769f6499e7ddcf29506705 HEAD 通过;gofmt -l v2-server/internal/workspacefs/reclaim_check.go v2-server/internal/workspacefs/reclaim_check_test.go v2-server/internal/workspacereclaim/reclaim_test.go 通过;go vet ./internal/workspacefs ./internal/workspacereclaim 通过;go test -race -count=1 ./internal/workspacefs -run TestInspectReclaim 通过;go test -race -count=1 ./internal/workspacereclaim -run TestSealOffersDiscardOnlyWithACompleteDigest 通过;go test -race -count=1 ./internal/workspacereclaim -run 'TestDiscardIsRefusedWhenNestedGitUsesSymlinksOrAlternates|TestDiscardStillAllowsWorkingTreeSymlinksInNestedRepository' 没跑(未配置 TEST_DATABASE_URL,集成用例均跳过。);没跑全量和 e2e。

涉及删除数据,建议 owner 再看一眼。

以上都是建议,怎么处理由你决定。

自动检视 · 基于 d896557 · 第 2 轮(增量)

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.

3 participants