-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathantigravity_settings.go
More file actions
189 lines (170 loc) · 4.69 KB
/
Copy pathantigravity_settings.go
File metadata and controls
189 lines (170 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package agent
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sync"
)
// Official agy settings path. There is no documented --settings flag or env
// override that headless print-mode honors; permissions are read from
// ~/.gemini/antigravity-cli/settings.json.
// See https://antigravity.google/docs/cli/permissions/
func defaultAntigravitySettingsPath() string {
home, err := os.UserHomeDir()
if err != nil || home == "" {
return ""
}
return filepath.Join(home, ".gemini", "antigravity-cli", "settings.json")
}
// antigravitySettingsPathForTest, when set, redirects settings writes away
// from the developer's real ~/.gemini tree (agent tests do not isolate HOME).
var antigravitySettingsPathForTest func() string
func antigravitySettingsPath() string {
if antigravitySettingsPathForTest != nil {
return antigravitySettingsPathForTest()
}
return defaultAntigravitySettingsPath()
}
// Inspect commands reviews run (pwd/wc/ls/...) must be allowlisted. In
// headless print mode, unconfigured command() actions default to Ask and
// are soft-denied or hard-fail with "permission check failed for command".
var antigravityReviewAllowPermissions = []string{
"read_file(*)",
"command(pwd)",
"command(wc)",
"command(ls)",
"command(cat)",
"command(head)",
"command(tail)",
"command(stat)",
"command(file)",
}
var antigravitySettingsMu sync.Mutex
// ensureAntigravityReviewPermissions merges the allow-rules non-agentic
// reviews need into settings.json. Existing keys and allow entries are
// preserved; only missing allow strings are appended. Invalid JSON is
// left untouched.
func ensureAntigravityReviewPermissions(settingsPath string) error {
if settingsPath == "" {
return nil
}
antigravitySettingsMu.Lock()
defer antigravitySettingsMu.Unlock()
doc := map[string]any{}
raw, err := os.ReadFile(settingsPath)
switch {
case err == nil:
if trimmed := trimSpaceBytes(raw); len(trimmed) > 0 {
if err := json.Unmarshal(raw, &doc); err != nil {
return fmt.Errorf("parse %s: %w", settingsPath, err)
}
}
case os.IsNotExist(err):
// create below
default:
return fmt.Errorf("read %s: %w", settingsPath, err)
}
permissions, err := settingsObject(doc, "permissions")
if err != nil {
return fmt.Errorf("%s: %w", settingsPath, err)
}
doc["permissions"] = permissions
allow, changed, err := mergeAllowList(permissions["allow"], antigravityReviewAllowPermissions)
if err != nil {
return fmt.Errorf("%s permissions.allow: %w", settingsPath, err)
}
if !changed && fileExists(settingsPath) {
return nil
}
permissions["allow"] = allow
if err := writeSettingsJSON(settingsPath, doc); err != nil {
return fmt.Errorf("write %s: %w", settingsPath, err)
}
return nil
}
func ensureAntigravityReviewSettings() {
path := antigravitySettingsPath()
if path == "" {
log.Printf("antigravity: skipping settings merge; cannot resolve home directory")
return
}
if err := ensureAntigravityReviewPermissions(path); err != nil {
log.Printf("antigravity: could not merge review permissions into %s: %v", path, err)
}
}
func settingsObject(doc map[string]any, key string) (map[string]any, error) {
raw, ok := doc[key]
if !ok || raw == nil {
return map[string]any{}, nil
}
obj, ok := raw.(map[string]any)
if !ok {
return nil, fmt.Errorf("%s is not a JSON object", key)
}
return obj, nil
}
func mergeAllowList(existing any, needed []string) (allow []any, changed bool, err error) {
switch v := existing.(type) {
case nil:
allow = nil
case []any:
allow = append([]any(nil), v...)
default:
return nil, false, fmt.Errorf("not a JSON array")
}
have := make(map[string]struct{}, len(allow))
for _, item := range allow {
s, ok := item.(string)
if !ok {
continue
}
have[s] = struct{}{}
}
for _, rule := range needed {
if _, ok := have[rule]; ok {
continue
}
allow = append(allow, rule)
have[rule] = struct{}{}
changed = true
}
if existing == nil {
changed = true
}
return allow, changed, nil
}
func writeSettingsJSON(path string, doc map[string]any) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
}
data = append(data, '\n')
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o644); err != nil {
return err
}
if err := os.Rename(tmp, path); err != nil {
_ = os.Remove(tmp)
return err
}
return nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func trimSpaceBytes(b []byte) []byte {
i, j := 0, len(b)
for i < j && (b[i] == ' ' || b[i] == '\n' || b[i] == '\r' || b[i] == '\t') {
i++
}
for j > i && (b[j-1] == ' ' || b[j-1] == '\n' || b[j-1] == '\r' || b[j-1] == '\t') {
j--
}
return b[i:j]
}