跳到主要内容

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

浏览文件

host.go

115 行 · 3.2 KB历史下载
1// Package host is what this machine can give a VM.
2package host
3
4import (
5 "fmt"
6 "io"
7 "os"
8 "path/filepath"
9 "regexp"
10 "runtime"
11 "strconv"
12
13 "github.com/pbnjay/memory"
14 "github.com/sylvinus/agent-vm/internal/paths"
15)
16
17// A VM handed more CPU or RAM than the host can spare makes the host
18// unusable while the agent runs, unattended, for a while. So a --cpus or
19// --memory above this host's share is clamped to it, out loud. The share is
20// half the host by default: a policy, which AGENT_VM_HOST_SHARE overrides
21// (1: the whole host).
22
23// CPUs is the host's CPU count, 0 when it cannot be told.
24var CPUs = func() int { return runtime.NumCPU() }
25
26// MemGiB is the host's RAM in GiB, 0 when it cannot be told.
27var MemGiB = func() int { return int(memory.TotalMemory() >> 30) }
28
29var positiveRe = regexp.MustCompile(`^[1-9][0-9]*$`)
30
31// Divisor is the number the host's resources are divided by to get a VM's
32// share: AGENT_VM_HOST_SHARE, or 2 when it is unset or is not a positive
33// integer. In the second case invalid is true and bad is what the variable
34// holds.
35func Divisor() (div int, bad string, invalid bool) {
36 s, ok := os.LookupEnv("AGENT_VM_HOST_SHARE")
37 if !ok {
38 return 2, "", false
39 }
40 if n, err := strconv.Atoi(s); err == nil && positiveRe.MatchString(s) {
41 return n, "", false
42 }
43 return 2, s, true
44}
45
46// share is the share of total this host gives a VM, never below floor.
47func share(total, floor int, warn io.Writer) int {
48 div, bad, invalid := Divisor()
49 if invalid {
50 fmt.Fprintf(warn, "Warning: AGENT_VM_HOST_SHARE='%s' is not a positive integer; using 2.\n", bad)
51 }
52 return max(total/div, floor)
53}
54
55// Cap is the value of --cpus (kind "cpus") or --memory ("memory", GiB) to
56// apply: above this host's share, clamped, with a notice; else as asked. 0
57// (nothing asked) and a host that cannot be read leave it alone: guessing
58// low would hand a 1-CPU VM to a 64-core host.
59func Cap(kind string, val int, warn io.Writer) int {
60 if val == 0 {
61 return 0
62 }
63 total, floor := 0, 0
64 switch kind {
65 case "cpus":
66 total, floor = CPUs(), 1
67 case "memory":
68 total, floor = MemGiB(), 2
69 default:
70 return val
71 }
72 if total <= 0 {
73 return val
74 }
75 if s := share(total, floor, warn); val > s {
76 fmt.Fprintf(warn, "Note: --%s %d exceeds this host's share (%d detected); using %d.\n", kind, val, total, s)
77 return s
78 }
79 return val
80}
81
82// FreeGiB reports available space on the physical volume where dir will
83// be created. A first-run Lima home can be a missing child of a junction.
84func FreeGiB(dir string) (int, bool) {
85 for {
86 real, err := paths.Resolve(dir)
87 if err == nil {
88 return freeGiB(real)
89 }
90 if !os.IsNotExist(err) {
91 return 0, false
92 }
93 if _, err = os.Lstat(dir); !os.IsNotExist(err) {
94 return 0, false
95 }
96 parent := filepath.Dir(dir)
97 if parent == dir {
98 return 0, false
99 }
100 dir = parent
101 }
102}
103
104// WarnDiskSpace warns when the file system of dir has less free space than
105// a disk of want GiB. Disks are sparse, allocated as they fill: a warning,
106// not an error.
107func WarnDiskSpace(dir string, want int, warn io.Writer) {
108 if want == 0 {
109 return
110 }
111 free, ok := FreeGiB(dir)
112 if ok && free < want {
113 fmt.Fprintf(warn, "Warning: ~%d GiB free for a %d GiB VM disk (sparse: allocated as used).\n", free, want)
114 }
115}