Repository navigation
Expand file tree
/
Copy pathmerge.rs
More file actions
236 lines (216 loc) · 8.39 KB
/
Copy pathmerge.rs
File metadata and controls
236 lines (216 loc) · 8.39 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::key_aliases::normalize_key_aliases;
use crate::key_aliases::normalized_with_key_aliases;
use codex_network_proxy::normalize_host;
use toml::Value as TomlValue;
/// The mutually exclusive shell-environment filter representations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShellEnvironmentPolicyFilterRepresentation {
Filters,
Legacy,
}
impl ShellEnvironmentPolicyFilterRepresentation {
/// Returns the representation selected by a policy table, including empty fields.
pub fn from_policy(policy: &TomlValue) -> Option<Self> {
let policy = policy.as_table()?;
if policy.contains_key("filters") {
Some(Self::Filters)
} else if policy.contains_key("exclude") || policy.contains_key("include_only") {
Some(Self::Legacy)
} else {
None
}
}
/// Returns the representation addressed by a dotted config path.
pub fn from_path(path: &[String]) -> Option<Self> {
match path {
[policy, field, ..] if policy == "shell_environment_policy" => match field.as_str() {
"filters" => Some(Self::Filters),
"exclude" | "include_only" => Some(Self::Legacy),
_ => None,
},
_ => None,
}
}
/// Returns the representation selected by an edit at `path`.
pub fn from_edit(path: &[String], value: &TomlValue) -> Option<Self> {
if matches!(path, [policy] if policy == "shell_environment_policy") {
Self::from_policy(value)
} else {
Self::from_path(path)
}
}
/// Returns the policy fields that must be removed when this representation is selected.
pub fn displaced_fields(self) -> &'static [&'static str] {
match self {
Self::Filters => &["exclude", "include_only"],
Self::Legacy => &["filters"],
}
}
}
/// Merge config `overlay` into `base`, giving `overlay` precedence.
pub fn merge_toml_values(base: &mut TomlValue, overlay: &TomlValue) {
merge_toml_values_at_path(base, overlay, &mut Vec::new());
}
pub fn is_structured_feature_path<S: AsRef<str>>(path: &[S]) -> bool {
let (features, feature) = match path {
[profiles, _, features, feature] if profiles.as_ref() == "profiles" => (features, feature),
[features, feature] => (features, feature),
_ => return false,
};
features.as_ref() == "features"
&& matches!(
feature.as_ref(),
"code_mode" | "multi_agent_v2" | "network_proxy" | "sleep_tool"
)
}
fn merge_toml_values_at_path(base: &mut TomlValue, overlay: &TomlValue, path: &mut Vec<String>) {
replace_shell_environment_policy_filter_representation(base, overlay, path);
if is_structured_feature_path(path) {
if let TomlValue::Boolean(enabled) = base
&& overlay.is_table()
{
*base = TomlValue::Table(toml::map::Map::from_iter([(
"enabled".to_string(),
TomlValue::Boolean(*enabled),
)]));
} else if let TomlValue::Table(table) = base
&& let TomlValue::Boolean(enabled) = overlay
{
table.insert("enabled".to_string(), TomlValue::Boolean(*enabled));
return;
}
}
if let TomlValue::Table(overlay_table) = overlay
&& let TomlValue::Table(base_table) = base
{
normalize_key_aliases(path, base_table);
let mut overlay_table = overlay_table.clone();
normalize_key_aliases(path, &mut overlay_table);
if is_network_domains_path(path) {
normalize_network_domain_keys(base_table);
normalize_network_domain_keys(&mut overlay_table);
}
if is_shell_environment_filters_path(path) {
normalize_case_insensitive_keys(base_table);
normalize_case_insensitive_keys(&mut overlay_table);
}
if path.split_last().is_some_and(|(field, feature_path)| {
field == "credentials" && is_structured_feature_path(feature_path)
}) {
for (provider_id, provider) in &overlay_table {
let Some(overlay_sources) = provider.get("env").and_then(TomlValue::as_array)
else {
continue;
};
base_table.retain(|existing_id, existing| {
existing_id == provider_id
|| !overlay_table
.get(existing_id)
.and_then(|provider| provider.get("env"))
.or_else(|| existing.get("env"))
.and_then(TomlValue::as_array)
.is_some_and(|existing_sources| {
existing_sources.iter().filter_map(TomlValue::as_str).any(
|existing_source| {
overlay_sources.iter().filter_map(TomlValue::as_str).any(
|overlay_source| {
existing_source == overlay_source
|| cfg!(windows)
&& existing_source
.eq_ignore_ascii_case(overlay_source)
},
)
},
)
})
});
}
}
for (key, value) in overlay_table {
path.push(key.clone());
if let Some(existing) = base_table.get_mut(&key) {
merge_toml_values_at_path(existing, &value, path);
} else {
base_table.insert(key, normalized_with_key_aliases(&value, path));
}
path.pop();
}
} else {
*base = normalized_with_key_aliases(overlay, path);
}
}
fn is_shell_environment_filters_path(path: &[String]) -> bool {
matches!(
path,
[policy, filters]
if policy == "shell_environment_policy" && filters == "filters"
)
}
/// Switching between legacy arrays and keyed filters replaces lower filter
/// fields instead of attempting to reconcile the two representations. Legacy
/// arrays already replace wholesale, so reconciling them would add merge
/// semantics that the legacy representation never supported.
fn replace_shell_environment_policy_filter_representation(
base: &mut TomlValue,
overlay: &TomlValue,
path: &[String],
) {
if !matches!(path, [policy] if policy == "shell_environment_policy") {
return;
}
let Some(overlay_representation) =
ShellEnvironmentPolicyFilterRepresentation::from_policy(overlay)
else {
return;
};
let TomlValue::Table(base) = base else {
return;
};
for field in overlay_representation.displaced_fields() {
base.remove(*field);
}
}
/// Looks up a shell-environment filter pattern while ignoring case.
pub fn shell_environment_filter_entry<'a>(
root: &'a TomlValue,
path: &[String],
) -> Option<(&'a String, &'a TomlValue)> {
let [policy, filters, pattern] = path else {
return None;
};
if policy != "shell_environment_policy" || filters != "filters" {
return None;
}
let pattern = pattern.to_lowercase();
root.get(policy)?
.get(filters)?
.as_table()?
.iter()
.find(|(candidate, _)| candidate.to_lowercase() == pattern)
}
fn is_network_domains_path(path: &[String]) -> bool {
matches!(
path,
[permissions, _, network, domains]
if permissions == "permissions" && network == "network" && domains == "domains"
) || matches!(
path,
[application, network, domains]
if application == "application" && network == "network" && domains == "domains"
)
}
fn normalize_network_domain_keys(table: &mut toml::map::Map<String, TomlValue>) {
let entries = std::mem::take(table);
for (pattern, value) in entries {
table.insert(normalize_host(&pattern), value);
}
}
fn normalize_case_insensitive_keys(table: &mut toml::map::Map<String, TomlValue>) {
let entries = std::mem::take(table);
for (key, value) in entries {
table.insert(key.to_lowercase(), value);
}
}
#[cfg(test)]
#[path = "merge_tests.rs"]
mod tests;