跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

提交记录

保持剪贴板文件创建与缓存归属接口一致

固定源码快照中 clipboardfiles 与客机文件助手的 race 回归通过。

作者
Lee-xeo
时间
2026/11/09 14:26
提交
c9aabd3
上一个
5f1e75b
这个版本的全部文件
修改internal/clipboardfiles/archive.go+22 −4看文件
@@ -17,10 +17,16 @@ const SelectionLimit = 100
1717// LinkCachedFiles groups already frozen guest-cache files for one native file
1818// chooser without copying every attachment again. GUI callers validate ownership.
1919func LinkCachedFiles(paths []string) (result []string, err error) {
20新增: return linkCachedFiles(paths, temporaryDirectory)
21新增:}
22新增:
23新增:func temporaryDirectory() (string, error) { return os.MkdirTemp("", "cage-clipboard-") }
24新增:
25新增:func linkCachedFiles(paths []string, directory func() (string, error)) (result []string, err error) {
2026 if len(paths) == 0 || len(paths) > SelectionLimit {
2127 return nil, errors.New("请选择 1 至 100 个文件")
2228 }
23删除: root, err := os.MkdirTemp("", "cage-clipboard-")
29新增: root, err := directory()
2430 if err != nil {
2531 return nil, err
2632 }
@@ -51,10 +57,14 @@ func LinkCachedFiles(paths []string) (result []string, err error) {
5157// Save receives one file into a newly owned cache directory. The caller can
5258// publish the returned path only after the complete bounded write succeeds.
5359func Save(input io.Reader, name string) (path string, err error) {
60新增: return save(input, name, temporaryDirectory)
61新增:}
62新增:
63新增:func save(input io.Reader, name string, directory func() (string, error)) (path string, err error) {
5464 if !validName(name) || strings.Contains(name, ":") {
5565 return "", errors.New("剪贴板文件名称无效")
5666 }
57删除: root, err := os.MkdirTemp("", "cage-clipboard-")
67新增: root, err := directory()
5868 if err != nil {
5969 return "", err
6070 }
@@ -140,7 +150,11 @@ func Write(output io.Writer, paths []string) error {
140150}
141151
142152func Read(input io.Reader) (paths []string, err error) {
143删除: root, err := os.MkdirTemp("", "cage-clipboard-")
153新增: return read(input, temporaryDirectory)
154新增:}
155新增:
156新增:func read(input io.Reader, directory func() (string, error)) (paths []string, err error) {
157新增: root, err := directory()
144158 if err != nil {
145159 return nil, err
146160 }
@@ -206,11 +220,15 @@ func Read(input io.Reader) (paths []string, err error) {
206220}
207221
208222func Copy(paths []string) ([]string, error) {
223新增: return copyFiles(paths, temporaryDirectory)
224新增:}
225新增:
226新增:func copyFiles(paths []string, directory func() (string, error)) ([]string, error) {
209227 reader, writer := io.Pipe()
210228 defer reader.Close()
211229 done := make(chan error, 1)
212230 go func() { err := Write(writer, paths); writer.CloseWithError(err); done <- err }()
213删除: copied, err := Read(reader)
231新增: copied, err := read(reader, directory)
214232 reader.Close()
215233 if writeErr := <-done; writeErr != nil {
216234 if len(copied) > 0 {
修改internal/clipboardfiles/ownership_test.go+63 −0看文件
@@ -1,10 +1,15 @@
11package clipboardfiles
22
33import (
4新增: "bufio"
5新增: "context"
6新增: "fmt"
47 "os"
8新增: "os/exec"
59 "path/filepath"
610 "strings"
711 "testing"
12新增: "time"
813)
914
1015func cacheFixture(t *testing.T) (CacheStore, CachedSelection) {
@@ -120,3 +125,61 @@ func TestCacheConcurrentPublishCannotRecreateAReleasedReceipt(t *testing.T) {
120125 }
121126 }
122127}
128新增:
129新增:type producerInput struct{}
130新增:
131新增:func (producerInput) Read(buffer []byte) (int, error) {
132新增: fmt.Fprintln(os.Stdout, "copy-started")
133新增: return os.Stdin.Read(buffer)
134新增:}
135新增:
136新增:func TestCacheProducerFixture(t *testing.T) {
137新增: directory := os.Getenv("CAGE_CLIPBOARD_PRODUCER_TEST")
138新增: if directory == "" {
139新增: t.Skip("subprocess fixture")
140新增: }
141新增: _, _ = (CacheStore{Directory: directory}).Read(producerInput{})
142新增:}
143新增:
144新增:func TestCacheRecoversAProducerKilledDuringItsFirstRead(t *testing.T) {
145新增: root := t.TempDir()
146新增: store := CacheStore{Directory: filepath.Join(root, "records")}
147新增: ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
148新增: defer cancel()
149新增: child := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestCacheProducerFixture$")
150新增: child.Env = append(os.Environ(), "CAGE_CLIPBOARD_PRODUCER_TEST="+store.Directory, "TMPDIR="+root, "TEMP="+root, "TMP="+root)
151新增: input, err := child.StdinPipe()
152新增: if err != nil {
153新增: t.Fatal(err)
154新增: }
155新增: defer input.Close()
156新增: output, err := child.StdoutPipe()
157新增: if err != nil {
158新增: t.Fatal(err)
159新增: }
160新增: if err := child.Start(); err != nil {
161新增: t.Fatal(err)
162新增: }
163新增: defer child.Process.Kill()
164新增: line, err := bufio.NewReader(output).ReadString('\n')
165新增: if err != nil || strings.TrimSpace(line) != "copy-started" {
166新增: t.Fatal("producer did not enter its blocking read", err)
167新增: }
168新增: if listed, err := store.List(); err != nil || len(listed) != 0 {
169新增: t.Fatal("the live producer lost ownership", err)
170新增: }
171新增: if err := child.Process.Kill(); err != nil {
172新增: t.Fatal(err)
173新增: }
174新增: _ = child.Wait()
175新增: listed, err := store.List()
176新增: if err != nil || len(listed) != 1 {
177新增: t.Fatal("terminated producer had no creation receipt", err)
178新增: }
179新增: if err := store.Release(listed[0].ID); err != nil {
180新增: t.Fatal(err)
181新增: }
182新增: if _, err := os.Stat(filepath.Dir(listed[0].Paths[0])); !os.IsNotExist(err) {
183新增: t.Fatal("terminated producer retained its partial directory")
184新增: }
185新增:}