diff --git a/v2-server/README.md b/v2-server/README.md index 317d0061..a7ad62a1 100644 --- a/v2-server/README.md +++ b/v2-server/README.md @@ -698,7 +698,7 @@ not execute these kinds: `crates/application/src/workflow_run/engine/node_type.r 用户删除普通对话时,它的工作目录在成果能从回合交付恢复、或用户选了 `discard` 时才删除。判定、状态与运维见 [`internal/workspacereclaim`](internal/workspacereclaim): - **能恢复**(按登记的 session 直接查 `v2_execution_turns` / `v2_turn_deliveries` / `v2_turn_delivery_work`,对话行删了也能判):有交付记录(否则 `legacy`);没有未结束的回合和交付义务(否则 `delivering`);最后一轮的交付是 `delivered`/`empty`(否则 `last_turn_undelivered`);Agent 仓库没有 `refs/stash`、`refs/remotes/` 之外的 ref 都剥成提交且不超出起点与 `HEAD` 的祖先(伪 ref 如 `ORIG_HEAD` 不算)、快照里没有与起点不同的 gitlink、顶层以下没有任何名为 `.git` 的条目(目录、gitfile、链接都算,被忽略的目录也扫)、`.git/modules/` 下没有已初始化的子模块、HEAD 是起点或其祖先或它跟踪的每个路径都与交付树或起点树一致、index 没有冲突且每个条目都与交付树、起点树或(未暂存时)HEAD 一致(否则 `git_state`,预览的 `git` 列出 stash 条数、各 ref 超出的提交数、嵌套仓库路径、`headDiffers`/`headCommits`、`indexDiffers`);`.git` 解析不出来、HEAD 无法解析成可读的提交(只有直接目标分支不存在的 unborn HEAD 可跳过,符号引用不递归)、磁盘上记录的 ref(loose 与 packed-refs,只看 Git 会当作 ref 的名字,过期的 `.lock` 等跳过)格式不对或 Git 读不出来、Git 明确报对象缺失或损坏,都判 `unverifiable`;I/O、权限、超时等其他 Git 失败按 `check_failed` 重试;持交付的目录锁按交付快照规则重新生成树(只写临时目录),与最后一轮交付的树(`empty` 时起点的树)相同(否则 `changed_after_delivery`,生成不了为 `unverifiable`)。 -- **预览**:`workspace`(`none` 附 `no_workspace` / `workspace_missing`;`recoverable`;`undelivered` 附上面的原因)、`ignored`、`undeliveredChanges`(相对最近一次 `delivered` 交付,没有时相对起点,至多 200 条)与 `undeliveredTotal`、`latestDelivery`(页面经 `…/workspace/deliveries/{id}/patch` 下载)、`revision`(最后一轮、交付、判定、快照树、Git 统计,以及 discard 会删掉、交付又带不走的全部 Git 相关内容的字节摘要:顶层仓库的 ref/HEAD/stash OID 与 index 内容,每个嵌套仓库整棵子树(工作树与 `.git`,含对象)的内容,gitfile、链接或 `.git` 目录里的 `commondir` 指向 checkout 内的 Git 目录时连同目标整棵(同一目录只算一次);任何字节变化都会改变它)、`discardable`(只有未交付且内容摘要完整时为 true;字节或条目超限、特殊文件、读取失败、嵌套 Git 目录内含符号链接或 `alternates`、指针指向 checkout 外或解析不了都为 false,没有检查就为 false;此时 discard 返回 409 `discard_unverifiable`,界面不给丢弃选项)。 +- **预览**:`workspace`(`none` 附 `no_workspace` / `workspace_missing`;`recoverable`;`undelivered` 附上面的原因)、`ignored`、`undeliveredChanges`(相对最近一次 `delivered` 交付,没有时相对起点,至多 200 条)与 `undeliveredTotal`、`latestDelivery`(页面经 `…/workspace/deliveries/{id}/patch` 下载)、`revision`(最后一轮、交付、判定、快照树、Git 统计,以及 discard 会删掉、交付又带不走的全部 Git 相关内容的字节摘要:顶层仓库的 ref/HEAD/stash OID 与 index 内容,每个嵌套仓库整棵子树(工作树与 `.git`,含对象)的内容,gitfile、链接或 `.git` 目录里的 `commondir` 指向 checkout 内的 Git 目录时连同目标整棵(同一目录只算一次),`.git/modules/` 也连同它指向的目录,单个仓库子树内的 Git 元数据不在遍历后再单独复扫;任何字节变化都会改变它)、`discardable`(只有未交付且内容摘要完整时为 true;字节或条目超限、特殊文件、读取失败、嵌套 Git 目录内含符号链接或特殊文件、任意深度的 `alternates`(本身叫这个名字的 Git 目录不算)、`.git/modules/` 里的 `commondir`、Git 目录解析到 checkout 根、指针指向 checkout 外或解析不了都为 false,没有检查就为 false;此时 discard 返回 409 `discard_unverifiable`,界面不给丢弃选项;会因此被拒绝的合法仓库见 [`internal/workspacefs`](internal/workspacefs) 的已知限制)。 - **删除**:删除请求在删除事务提交后用服务生命周期的上下文(不随请求取消)启动回收,5 秒内删完返回 `reclaimed`,否则返回 `pending`/`in_progress`、后台继续删完(关机时取消,记录留 `deleting` 给启动);事务提交后的任何错误都只体现在结果里,不报成删除失败。`reclaim` 不需要 `revision`,删除时重新判定,结果只会更保守。`discard` 必须带回预览的 `revision`,在结束会话之前重新计算,不一致或还在交付返回 409 `deletion_preview_stale`,对话和目录都不动。返回 `workspace`:`reclaimed`(附 `releasedBytes`、`releasedFiles`)、`kept`(附原因)、`pending`(`in_progress` 后台继续;`delivering`、`removal_failed`、`check_failed` 下次启动再做;`reclaim_unavailable` 等带运行时的启动)、`none`。 - **先登记再删**:`store.DeleteChatConversationReclaiming`(只有用户删除走它;首轮被拒回滚对话用的 `DeleteChatConversation` 不登记)在删除对话的同一事务里写一行 `v2_workspace_reclaims`:目录 ID、项目、session、路径、`clone_execution`、模式(不可变)。状态 `pending → deleting → reclaimed | kept`;`deleting` 之后只重查身份与路径,不再判定。启动时在 `boot.Recover` 里 `RecoverTurnDeliveries` 之后、恢复工作流之前,把 `pending` / `deleting` 的再执行一遍;运行中不另外触发,不加定时任务。同一条回收在进程内按目录 ID 互斥。 - **保留与重试**:`reclaim` 下不能恢复、身份不符(`identity_mismatch`:路径或 `clone_execution` 与 `v2_workspaces` 不一致或路径被别的行记录)、别的 session 用过(`shared`)、仍有活 session(`session_live`)、路径不安全(`unsafe_path`)记 `kept`;检查时(读工作目录、镜像、Git 失败,`check_failed`)或删除时(`removal_failed`)的基础设施错误保持原状态,`attempts` 加一、记 `last_error` 与日志,下次启动重试;只有目录内容本身无法验证(Git 元数据不能安全读取、改动过大、分不清物化技能)才判 `unverifiable`。 diff --git a/v2-server/internal/workspacefs/README.md b/v2-server/internal/workspacefs/README.md index e79d693f..b3f12f03 100644 --- a/v2-server/internal/workspacefs/README.md +++ b/v2-server/internal/workspacefs/README.md @@ -16,6 +16,8 @@ ## 删除对话时的检查与删除 -`InspectReclaim` 持目录锁按交付的快照规则在临时目录生成树(不写镜像、不写 ref),与给定交付提交(空时起点)的树比对,列出差异(至多 `MaxReclaimChanges` 条,另给总数),并查交付带不走的 Git 状态(`refs/stash`、`refs/remotes/` 之外超出起点与 `HEAD` 的本地 ref、与起点不同的 gitlink、嵌套 `.git`)和有无被忽略规则排除的文件(不算物化的技能),并带出快照树 OID 和 Git 身份摘要供预览的 `revision` 绑定内容。身份摘要绑定 discard 会删掉、交付带不走的全部 Git 相关字节:顶层仓库的 ref/HEAD/stash OID 与 index 内容,每个嵌套仓库整棵子树(含 `.git` 与对象)的内容,gitfile、链接或 `.git` 目录里的 `commondir` 指向 checkout 内的 Git 目录时连同目标整棵(同一目录只算一次)。读取不调用 Git、不跟随链接:先 `Lstat` 只接受普通文件,再以 `O_NOFOLLOW|O_NONBLOCK` 打开并 `fstat` 复核;嵌套 Git 目录(含 `.git/modules/` 与解析后的 Git 目录)内出现符号链接或 `objects/info/alternates` 时同样不跟随,直接将摘要标记为不完整。内容字节共用交付的对象上限、条目至多 10 万;任何超限、特殊文件、读取失败、嵌套 Git 目录内的符号链接或 `alternates`、指向 checkout 外或解析不了的指针都让摘要不完整(`GitIdentityComplete` 为 false),且不会再恢复。整个检查受 2 分钟上限约束。目录内容无法验证时返回 `ErrUnverifiable`,其余错误(磁盘、镜像、Git)可重试。 +`InspectReclaim` 持目录锁按交付的快照规则在临时目录生成树(不写镜像、不写 ref),与给定交付提交(空时起点)的树比对,列出差异(至多 `MaxReclaimChanges` 条,另给总数),并查交付带不走的 Git 状态(`refs/stash`、`refs/remotes/` 之外超出起点与 `HEAD` 的本地 ref、与起点不同的 gitlink、嵌套 `.git`)和有无被忽略规则排除的文件(不算物化的技能),并带出快照树 OID 和 Git 身份摘要供预览的 `revision` 绑定内容。身份摘要绑定 discard 会删掉、交付带不走的全部 Git 相关字节:顶层仓库的 ref/HEAD/stash OID 与 index 内容,每个嵌套仓库整棵子树(含 `.git` 与对象)的内容,gitfile、链接或 `.git` 目录里的 `commondir` 指向 checkout 内的 Git 目录时连同目标整棵(同一目录只算一次),`.git/modules/` 也连同它指向的目录。单个仓库子树内的 Git 元数据不再单独复扫:Git 目录先解析出来,再在同一次遍历里按更严的规则检查(Git 目录落在另一个仓库已摘要的子树里时仍单独扫一遍;嵌套仓库互相包含时,重叠的子树仍按仓库各算一次)。读取不调用 Git、不跟随链接:先 `Lstat` 只接受普通文件,再以 `O_NOFOLLOW|O_NONBLOCK` 打开并 `fstat` 复核;嵌套 Git 目录(含 `.git/modules/` 与解析后的 Git 目录)内出现符号链接或特殊文件、任意深度的 `objects/info/alternates`(本身就叫这个名字的 Git 目录不算,如 `git init --separate-git-dir cache/objects/info/alternates`)、`.git/modules/` 里的 `commondir`,或 Git 目录解析到 checkout 根时,都不跟随,直接将摘要标记为不完整。内容字节共用交付的对象上限、条目至多 10 万;任何超限、特殊文件、读取失败、上面这几种情况、指向 checkout 外或解析不了的指针都让摘要不完整(`GitIdentityComplete` 为 false),且不会再恢复。整个检查受 2 分钟上限约束。目录内容无法验证时返回 `ErrUnverifiable`,其余错误(磁盘、镜像、Git)可重试。 + +已知限制:这些规则也拒绝一些本身合法的仓库,它们都要交运维处理,而不是让用户丢弃——嵌套仓库把 hook 放成符号链接(`hooks/` 或 `core.hooksPath` 指向的是链接)、用 `clone --shared` / `--reference` 或子模块的 `alternateLocation` 共享对象库(`objects/info/alternates`)、`.git/modules/` 里有 `commondir`(多工作树形式的子模块);嵌套仓库工作树里的普通符号链接不受影响。 `RemoveCheckout(root, path)` 只删除位于 checkout 根 `root`(`<项目目录>/w/r`)之下的 `path`:从 `root` 起经目录句柄逐级打开,每级先 `Lstat` 确认是目录而非链接、打开后比对 dev/inode;统计、补属主写权限、删内容(链接只删本身)经 checkout 句柄,最后经父目录句柄删掉 checkout 本身,检查之后不再按路径重开。读不到的条目返回带路径的错误,列出后消失的不计。不安全时返回 `ErrUnsafeCheckout`;路径或根已不存在视为已删除。 diff --git a/v2-server/internal/workspacefs/reclaim_check.go b/v2-server/internal/workspacefs/reclaim_check.go index 293911a7..3b81d2d0 100644 --- a/v2-server/internal/workspacefs/reclaim_check.go +++ b/v2-server/internal/workspacefs/reclaim_check.go @@ -72,8 +72,11 @@ type ReclaimInspection struct { // GitIdentity digests the actual Git state behind Git (stash entries, ref targets, HEAD, // nested repositories), so a revision changes when that state does even if the counts do not. GitIdentity string - // GitIdentityComplete is false when a nested repository was too large to digest in full: the - // identity then cannot vouch for what discarding the checkout would drop. + // GitIdentityComplete is false when the identity could not bind every byte a discard would + // delete: a size or entry limit, a special file, a failed read, a pointer leading outside the + // checkout or not resolving, or a symlink, special file, or objects/info/alternates inside a + // nested Git directory. The identity then cannot vouch for what discarding the checkout would + // drop, so a discard is refused. GitIdentityComplete bool // Ignored reports files the ignore rules (.gitignore and .git/info/exclude) leave out of // every delivery, not counting the skills Ora materialized. @@ -247,6 +250,11 @@ func (s *DeliveryFS) gitState(ctx context.Context, st *staged, target string) (G // delivery's object limit. Beyond either the digest is incomplete. const maxNestedEntries = 100000 +// alternatesSuffix names the file a repository uses to keep its objects elsewhere. One at any +// depth inside a Git directory cannot be bound; a Git directory that happens to be called like +// one is fine, which is why the path before it must also lie inside the Git directories. +const alternatesSuffix = "/objects/info/alternates" + // gitIdentity digests the actual bytes of everything Git-related that a discard would delete and // no delivery carries: the checkout repository's refs (name, target, peeled target), HEAD, stash // reflog entries and index; and for every nested repository its whole subtree (working tree and @@ -256,8 +264,9 @@ const maxNestedEntries = 100000 // // complete reports that every such byte was bound. It is false, and stays false, when anything // could not be: a byte or entry limit, a special file, a read that failed, a pointer leading out -// of the checkout or not resolving, or any symlink or objects/info/alternates inside a nested Git -// directory (including .git/modules entries and resolved Git directories). A discard is then +// of the checkout or not resolving, or any symlink, special file, or objects/info/alternates at +// any depth inside a nested Git directory (including .git/modules entries and resolved Git +// directories). A discard is then // refused, since the revision cannot prove the person saw what would be deleted. Files are read // without Git and without following links: an Lstat must show a regular file, the open is // O_NOFOLLOW|O_NONBLOCK and an fstat must confirm the same file, so nothing can block on a FIFO @@ -335,34 +344,41 @@ func (s *DeliveryFS) gitIdentity(ctx context.Context, st *staged, g GitFindings) } continue } - d.tree(root, repo) - digested = append(digested, repo) - // The repository's Git directories may lie elsewhere: a gitfile or a link points at them, - // and a .git directory may name a shared one in commondir. Bound only when they are inside - // the checkout (and so deleted with it), each once; outside, or not resolving, a discard - // could not show what it drops. Symlinks and alternates inside nested Git directories are - // rejected instead of followed, so shared Git state outside .git cannot be hidden behind - // an ignored link. - if _, err := root.Lstat(path.Join(repo, ".git")); err != nil { - continue - } - layout, err := gitexclude.Resolve(filepath.Join(st.root, filepath.FromSlash(repo))) - if err != nil { - d.incomplete("resolve nested .git", err) - continue - } - for _, dir := range []string{layout.GitDir(), layout.CommonDir()} { - real, err := filepath.EvalSymlinks(dir) - if err != nil { - d.incomplete("resolve nested Git directory", err) - continue - } - rel, err := filepath.Rel(realRoot, real) - if err != nil || !filepath.IsLocal(rel) || rel == "." { - d.incomplete("nested Git directory outside the checkout", nil) - continue + // Resolve the repository's Git directories before walking it: a gitfile or a link points at + // them, and a .git directory may name a shared one in commondir. They are bound once, by the + // same walk that covers the repository's subtree when they lie inside it; elsewhere in the + // checkout they are bound on their own. Either way they are read strictly, since symlinks or + // alternates inside them would hide Git state that a discard drops. Outside the checkout, or + // not resolving, a discard could not show what it drops. + var inside, elsewhere []string + if _, err := root.Lstat(path.Join(repo, ".git")); err == nil { + if layout, err := gitexclude.Resolve(filepath.Join(st.root, filepath.FromSlash(repo))); err != nil { + d.incomplete("resolve nested .git", err) + } else { + for _, dir := range []string{layout.GitDir(), layout.CommonDir()} { + real, err := filepath.EvalSymlinks(dir) + if err != nil { + d.incomplete("resolve nested Git directory", err) + continue + } + rel, err := filepath.Rel(realRoot, real) + if err != nil || !filepath.IsLocal(rel) || rel == "." { + d.incomplete("nested Git directory outside the checkout", nil) + continue + } + rel = filepath.ToSlash(rel) + if within(rel, []string{repo}) { + inside = append(inside, rel) + } else { + elsewhere = append(elsewhere, rel) + } + } } - rel = filepath.ToSlash(rel) + } + d.repoTree(root, repo, inside) + digested = append(digested, repo) + checkedGitDirs = append(checkedGitDirs, inside...) + for _, rel := range elsewhere { if !within(rel, digested) { fmt.Fprintf(h, "points at %s\x00", rel) d.gitTree(root, rel) @@ -453,10 +469,12 @@ func (d *digester) file(r *os.Root, name string) { d.bytes -= n } -// tree writes every entry below dir (dir itself included, .git directories included): regular -// files by content, links by target, directories by name. Any other kind of entry cannot be bound. -func (d *digester) tree(r *os.Root, dir string) { - 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. +// So the Git metadata inside this subtree is validated by the walk that hashes it, instead of +// being scanned again afterwards. +func (d *digester) repoTree(r *os.Root, dir string, gitDirs []string) { + d.walkTree(r, dir, gitDirs, true) } // gitTree writes every entry below a nested Git directory by content and fails closed when any @@ -472,12 +490,16 @@ func (d *digester) gitTree(r *os.Root, dir string) { d.incomplete(dir+" is not a directory", nil) return } - d.walkTree(r, dir, false, path.Join(dir, "objects/info/alternates")) + d.walkTree(r, dir, []string{dir}, false) } -func (d *digester) walkTree(r *os.Root, dir string, allowSymlinks bool, alternates string) { +// walkTree writes every entry below dir. Inside the directories in strict (which include dir +// itself when it is a Git directory) a delivery cannot carry what Git reads there, so a symlink or +// special file, and an objects/info/alternates at any depth, make the digest incomplete instead of +// being followed. allowMissing writes a marker for a gitlink that has no working tree here. +func (d *digester) walkTree(r *os.Root, dir string, strict []string, allowMissing bool) { if _, err := r.Lstat(dir); errors.Is(err, fs.ErrNotExist) { - if allowSymlinks { + if allowMissing { fmt.Fprintf(d.h, "missing %s\x00", dir) return // a gitlink with no working tree here } @@ -495,15 +517,16 @@ func (d *digester) walkTree(r *os.Root, dir string, allowSymlinks bool, alternat d.incomplete("too many entries", nil) return fs.SkipAll } - if alternates != "" && p == alternates { + if parent, ok := strings.CutSuffix(p, alternatesSuffix); ok && within(parent, strict) { d.incomplete(p+" uses alternates in a nested Git directory", nil) return fs.SkipAll } + inGit := strict != nil && within(p, strict) switch t := e.Type(); { case t.IsDir(): fmt.Fprintf(d.h, "dir %s\x00", p) case t&fs.ModeSymlink != 0: - if !allowSymlinks { + if inGit { d.incomplete(p+" is a symlink in a nested Git directory", nil) return fs.SkipAll } @@ -544,7 +567,7 @@ func (d *digester) checkGitDir(r *os.Root, dir string) { d.incomplete(dir+" is not a directory", nil) return } - alternates := path.Join(dir, "objects/info/alternates") + // A Git directory's alternates may sit at any depth (a deinitialized submodule keeps its own). checked := 0 err = fs.WalkDir(r.FS(), dir, func(p string, e fs.DirEntry, err error) error { if err != nil { @@ -557,7 +580,7 @@ func (d *digester) checkGitDir(r *os.Root, dir string) { d.incomplete("too many entries", nil) return fs.SkipAll } - if p == alternates { + if parent, ok := strings.CutSuffix(p, alternatesSuffix); ok && within(parent, []string{dir}) { d.incomplete(p+" uses alternates in a nested Git directory", nil) return fs.SkipAll } diff --git a/v2-server/internal/workspacefs/reclaim_check_test.go b/v2-server/internal/workspacefs/reclaim_check_test.go index 87049696..3e696147 100644 --- a/v2-server/internal/workspacefs/reclaim_check_test.go +++ b/v2-server/internal/workspacefs/reclaim_check_test.go @@ -600,6 +600,77 @@ func TestInspectReclaimMarksSymlinksAndAlternatesInNestedGitIncomplete(t *testin }, wantRepo: "wt", }, + "alternates deeper inside a nested Git directory": { + setup: func(t *testing.T, root string) { + deliveryWrite(t, root, ".gitignore", "*.log\nstore.git/\nwt/\n") + lib := initLib(t) + shared := filepath.Join(root, "store.git") + deliveryGit(t, root, "clone", "-q", "--bare", lib, shared) + wt := filepath.Join(root, "wt") + deliveryWrite(t, wt, "lib.go", "package lib\n") + deliveryGit(t, wt, "init", "-q", ".") + deliveryGit(t, wt, "add", ".") + deliveryGit(t, wt, "commit", "-qm", "lib") + deliveryGit(t, wt, "-c", "protocol.file.allow=always", "submodule", "add", "-q", lib, "mod") + deliveryGit(t, wt, "commit", "-qm", "submodule") + // Deinit: the submodule has no working tree, but its Git directory stays. + if err := os.RemoveAll(filepath.Join(wt, "mod")); err != nil { + t.Fatal(err) + } + const target = "../../../../../store.git/objects" + if _, err := os.Stat(filepath.Join(wt, ".git/modules/mod/objects", target)); err != nil { + t.Fatalf("the shared object store is not reachable through the alternates: %v", err) + } + deliveryWrite(t, wt, ".git/modules/mod/objects/info/alternates", target+"\n") + }, + wantRepo: "wt", + }, + "Git directory named like an alternates file stays complete": { + setup: func(t *testing.T, root string) { + deliveryWrite(t, root, ".gitignore", "*.log\nwt/\n") + // git init --separate-git-dir puts the metadata where an alternates file would sit; + // that is a Git directory, not a pointer at objects elsewhere. + wt := filepath.Join(root, "wt") + deliveryWrite(t, wt, "lib.go", "package lib\n") + if err := os.MkdirAll(filepath.Join(wt, "cache", "objects", "info"), 0o700); err != nil { + t.Fatal(err) + } + deliveryGit(t, wt, "init", "-q", "--separate-git-dir", "cache/objects/info/alternates", ".") + deliveryGit(t, wt, "add", ".") + deliveryGit(t, wt, "commit", "-qm", "lib") + }, + wantComplete: true, + wantRepo: "wt", + }, + "nested Git directory resolving to the checkout root": { + setup: func(t *testing.T, root string) { + deliveryWrite(t, root, ".gitignore", "*.log\nwt/\n") + wt := filepath.Join(root, "wt") + deliveryWrite(t, wt, "lib.go", "package lib\n") + deliveryGit(t, wt, "init", "-q", ".") + deliveryGit(t, wt, "add", ".") + deliveryGit(t, wt, "commit", "-qm", "lib") + if err := os.RemoveAll(filepath.Join(wt, ".git")); err != nil { + t.Fatal(err) + } + if err := os.Symlink("..", filepath.Join(wt, ".git")); err != nil { + t.Fatal(err) + } + }, + wantRepo: "wt", + }, + "alternates inside .git/modules entry": { + setup: func(t *testing.T, root string) { + deliveryWrite(t, root, ".gitignore", "*.log\nstore.git/\n") + lib := initLib(t) + shared := filepath.Join(root, "store.git") + deliveryGit(t, root, "clone", "-q", "--bare", lib, shared) + deliveryGit(t, root, "-c", "protocol.file.allow=always", "submodule", "add", "-q", lib, "mod") + deliveryGit(t, root, "commit", "-qm", "submodule") + deliveryWrite(t, root, ".git/modules/mod/objects/info/alternates", "../../../../store.git/objects\n") + }, + wantRepo: ".git/modules/mod", + }, "working tree symlink stays complete": { setup: func(t *testing.T, root string) { sub := filepath.Join(root, "vendor", "lib") diff --git a/v2-server/internal/workspacereclaim/reclaim_test.go b/v2-server/internal/workspacereclaim/reclaim_test.go index 533bd6fa..2fe51c49 100644 --- a/v2-server/internal/workspacereclaim/reclaim_test.go +++ b/v2-server/internal/workspacereclaim/reclaim_test.go @@ -1472,6 +1472,41 @@ func TestDiscardIsRefusedWhenNestedGitUsesSymlinksOrAlternates(t *testing.T) { } }) + t.Run("alternates behind a deinitialized submodule", func(t *testing.T) { + f := newFixture(t) + c := f.chat(t, st.TurnDelivered) + write(t, c.Path, ".gitignore", "*.log\nstore.git/\nwt/\n") + lib := initLib(t) + shared := filepath.Join(c.Path, "store.git") + git(t, c.Path, "clone", "-q", "--bare", lib, shared) + wt := filepath.Join(c.Path, "wt") + write(t, wt, "lib.txt", "lib\n") + git(t, wt, "init", "-q", ".") + git(t, wt, "add", "lib.txt") + git(t, wt, "commit", "-qm", "lib") + git(t, wt, "-c", "protocol.file.allow=always", "submodule", "add", "-q", lib, "mod") + git(t, wt, "commit", "-qm", "submodule") + // Deinit leaves .git/modules/mod in place without a working tree for it. + if err := os.RemoveAll(filepath.Join(wt, "mod")); err != nil { + t.Fatal(err) + } + const target = "../../../../../store.git/objects" + if _, err := os.Stat(filepath.Join(wt, ".git/modules/mod/objects", target)); err != nil { + t.Fatalf("the shared object store is not reachable through the alternates: %v", err) + } + write(t, wt, ".git/modules/mod/objects/info/alternates", target+"\n") + svc, seen := endAndPreview(t, f, c) + if seen.Workspace != wr.WorkspaceUndelivered || seen.Reason != wr.ReasonGitState || seen.Discardable { + t.Fatalf("preview = %+v", seen) + } + if _, err := svc.Delete(context.Background(), f.scope, c.ConversationID, store.ReclaimDiscard, seen.Revision); !errors.Is(err, chat.ErrDiscardUnverifiable) { + t.Fatalf("discard = %v, want ErrDiscardUnverifiable", err) + } + if _, err := f.s.ChatConversation(context.Background(), f.scope.TenantID, f.scope.UserID, c.ConversationID); err != nil || !exists(t, wt) { + t.Fatalf("conversation or wt missing after refused discard: %v", err) + } + }) + t.Run("symlink swapped in after preview", func(t *testing.T) { f := newFixture(t) c := f.chat(t, st.TurnDelivered)