use std::cell::OnceCell;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use similar::TextDiff;
use crate::access::{self, ReadOnlyKind, WriteAccess};
use crate::adapters::caps::{
DisableMech, ProjectDisable, ProjectSemantics, TimeUnit, TimeoutField, TrustGate,
};
use crate::adapters::detect::replacing_extension;
use crate::adapters::mapper::{Canonical, EntryMapper, WriteCx};
use crate::adapters::spec::{ContainerPath, McpSpec};
use crate::adapters::{ADAPTERS, AdapterSpec, Target, adapter};
use crate::apply::ledger::{
EntryKind, Ledger, LedgerEntry, LedgerKey, LedgerSlot, container_pointer,
};
use crate::apply::render::{
DesiredEntry, DesiredState, Detected, RenderCx, RenderTarget, Rendering, native_name,
render_for, system_entries, writable,
};
use crate::env::{Env, Os};
use crate::error::{CoreError, ErrorCode};
use crate::formats::{Document, Format, NativeValue};
use crate::layering::manifest::normalize_path_text;
use crate::layering::{
self, EntryBody, EntryLayer, KnownProject, Project, ProjectEntry, ProjectRepo, ProjectStatus,
TrustStore,
};
use crate::model::applied::Scope;
use crate::model::clients::ClientSelection;
use crate::model::server::Server;
use crate::paths::STATE_DIR;
use crate::secrets::SecretValue;
use crate::secrets::delivery::Sharing;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EntryState {
Synced,
PendingCreate,
PendingUpdate,
PendingRemove,
Drifted,
Missing,
Conflict,
Foreign,
Collision,
Blocked,
}
impl EntryState {
pub const fn as_str(self) -> &'static str {
match self {
EntryState::Synced => "synced",
EntryState::PendingCreate => "pending_create",
EntryState::PendingUpdate => "pending_update",
EntryState::PendingRemove => "pending_remove",
EntryState::Drifted => "drifted",
EntryState::Missing => "missing",
EntryState::Conflict => "conflict",
EntryState::Foreign => "foreign",
EntryState::Collision => "collision",
EntryState::Blocked => "blocked",
}
}
pub const fn is_pending(self) -> bool {
!matches!(
self,
EntryState::Synced | EntryState::Foreign | EntryState::Blocked
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
None,
Create,
Update,
Remove,
Disable,
Adopt,
AdoptOverwrite,
Refresh,
Forget,
Release,
Import,
Skip,
}
impl Action {
pub const fn as_str(self) -> &'static str {
match self {
Action::None => "none",
Action::Create => "create",
Action::Update => "update",
Action::Remove => "remove",
Action::Disable => "disable",
Action::Adopt => "adopt",
Action::AdoptOverwrite => "adopt_overwrite",
Action::Refresh => "refresh",
Action::Forget => "forget",
Action::Release => "release",
Action::Import => "import",
Action::Skip => "skip",
}
}
pub const fn writes(self) -> bool {
matches!(
self,
Action::Create
| Action::Update
| Action::Remove
| Action::Disable
| Action::AdoptOverwrite
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum OnConflict {
#[default]
Ask,
Skip,
Overwrite,
Import,
Release,
}
impl OnConflict {
pub const fn as_str(self) -> &'static str {
match self {
OnConflict::Ask => "ask",
OnConflict::Skip => "skip",
OnConflict::Overwrite => "overwrite",
OnConflict::Import => "import",
OnConflict::Release => "release",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Decision {
pub client: String,
pub name: String,
pub resolution: OnConflict,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ScopeFilter {
System,
Project,
#[default]
All,
}
impl ScopeFilter {
pub const fn as_str(self) -> &'static str {
match self {
ScopeFilter::System => "system",
ScopeFilter::Project => "project",
ScopeFilter::All => "all",
}
}
const fn system(self) -> bool {
matches!(self, ScopeFilter::System | ScopeFilter::All)
}
const fn project(self) -> bool {
matches!(self, ScopeFilter::Project | ScopeFilter::All)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ProjectSelection {
#[default]
All,
Only(PathBuf),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TargetFilter {
pub scope: Scope,
pub project: Option<PathBuf>,
pub file: PathBuf,
}
impl TargetFilter {
fn matches(&self, target: &Target) -> bool {
self.scope == target.scope
&& layering::same_path(&self.file, &target.file)
&& match (&self.project, &target.project) {
(None, None) => true,
(Some(a), Some(b)) => layering::same_path(a, b),
_ => false,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct PlanOptions {
pub clients: Option<Vec<String>>,
pub on_conflict: OnConflict,
pub decisions: Vec<Decision>,
pub scope: ScopeFilter,
pub projects: ProjectSelection,
pub targets: Option<Vec<TargetFilter>>,
pub servers: Option<Vec<String>>,
pub entries: Option<Vec<EntryFilter>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntryFilter {
pub target: TargetFilter,
pub pointer: String,
pub name: String,
}
impl PlanOptions {
fn selects_entry(&self, target: &Target, pointer: &str, name: &str) -> bool {
self.entries.as_ref().is_none_or(|entries| {
entries.iter().any(|entry| {
entry.name == name && entry.pointer == pointer && entry.target.matches(target)
})
})
}
fn resolution(&self, client: Option<&str>, name: &str) -> OnConflict {
self.decisions
.iter()
.find(|decision| Some(decision.client.as_str()) == client && decision.name == name)
.map_or(self.on_conflict, |decision| decision.resolution)
}
fn selects(&self, client: &str) -> bool {
self.clients
.as_ref()
.is_none_or(|clients| clients.iter().any(|id| id == client))
}
fn selects_target(&self, target: &Target) -> bool {
self.targets
.as_ref()
.is_none_or(|targets| targets.iter().any(|filter| filter.matches(target)))
}
fn selects_servers(
&self,
input: &TargetInput,
ledger: &Ledger,
derived: &[(String, String)],
) -> bool {
let Some(ids) = &self.servers else {
return true;
};
let named = |id: &str| ids.iter().any(|named| named == id);
if input.desired.iter().any(|entry| named(&entry.server_id))
|| input.members.iter().any(|member| named(&member.server_id))
|| input.sources.iter().any(|from| named(from))
{
return true;
}
let Ok(keys) = input.container_keys() else {
return false;
};
let file = input.target.file.to_string_lossy();
let pointer = container_pointer(&keys);
let slot = LedgerSlot {
scope: input.target.scope,
project_id: input.project_id.as_deref(),
file: &file,
container: &pointer,
};
ledger.slot_entries(&slot).iter().any(|row| {
row.server_id.as_deref().is_some_and(|id| {
named(id)
|| derived.iter().any(|(project, name)| {
row.project_id.as_deref() == Some(project.as_str()) && name == id
})
})
})
}
fn plans_user(&self) -> bool {
self.scope.system() && self.projects == ProjectSelection::All
}
fn plans_project(&self, project: &KnownProject) -> bool {
self.scope.project()
&& match &self.projects {
ProjectSelection::All => !matches!(project.status, ProjectStatus::Missing),
ProjectSelection::Only(root) => layering::same_path(&project.root, root),
}
}
}
#[derive(Debug, Clone)]
pub struct EntryPlan {
pub name: String,
pub client: Option<&'static str>,
pub server_id: Option<String>,
pub state: EntryState,
pub action: Action,
pub unresolved: bool,
pub problem: Option<CoreError>,
pub desired: Option<NativeValue>,
pub observed: Option<NativeValue>,
pub written: Option<NativeValue>,
pub delivery: Option<&'static str>,
}
#[derive(Debug, Clone)]
pub struct TargetPlan {
pub clients: Vec<&'static str>,
pub scope: Scope,
pub project_id: Option<String>,
pub project: Option<PathBuf>,
pub kind: EntryKind,
pub file: PathBuf,
pub container: Vec<String>,
pub pointer: String,
pub before: Option<Vec<u8>>,
pub after: Option<Vec<u8>>,
pub entries: Vec<EntryPlan>,
pub problem: Option<CoreError>,
pub warnings: Vec<CoreError>,
pub read_only: Option<ReadOnlyKind>,
pub(crate) masked: Vec<SecretValue>,
pub(crate) input: TargetInput,
}
#[derive(Debug, Clone)]
pub(crate) struct TargetInput {
pub(crate) target: Target,
pub(crate) clients: Vec<&'static str>,
pub(crate) desired: Vec<DesiredEntry>,
pub(crate) members: Vec<DesiredMember>,
pub(crate) native_disable: BTreeMap<&'static str, bool>,
pub(crate) invalid_servers: Vec<String>,
pub(crate) project_id: Option<String>,
pub(crate) kind: EntryKind,
pub(crate) keys: Option<Vec<String>>,
pub(crate) blocked: Option<CoreError>,
pub(crate) exclude: bool,
pub(crate) sources: Vec<String>,
pub(crate) access: WriteAccess,
}
#[derive(Debug, Clone)]
pub(crate) struct DesiredMember {
pub(crate) client: &'static str,
pub(crate) server_id: String,
pub(crate) name: String,
}
impl TargetInput {
fn new(target: Target, client: &'static str, native_disable: bool) -> Self {
Self {
target,
clients: vec![client],
desired: Vec::new(),
members: Vec::new(),
native_disable: BTreeMap::from([(client, native_disable)]),
invalid_servers: Vec::new(),
project_id: None,
kind: EntryKind::McpEntry,
keys: None,
blocked: None,
exclude: false,
sources: Vec::new(),
access: WriteAccess::Unknown,
}
}
fn container_keys(&self) -> Result<Vec<String>, CoreError> {
match &self.keys {
Some(keys) => Ok(keys.clone()),
None => self.target.container_keys(),
}
}
fn same(&self, target: &Target, kind: EntryKind) -> bool {
self.kind == kind
&& self.target.file == target.file
&& self.target.container == target.container
&& self.target.project == target.project
}
fn join(&mut self, client: &'static str, native_disable: bool, warnings: Vec<CoreError>) {
if !self.clients.contains(&client) {
self.clients.push(client);
self.target.warnings.extend(warnings);
}
self.native_disable.insert(client, native_disable);
}
fn without_ejected(&mut self, ejected: &crate::eject::Ejected) -> bool {
let project = self.project_id.clone();
let covered = |client: &str| ejected.covers(client, project.as_deref());
self.clients.retain(|client| !covered(client));
self.desired.retain(|entry| !covered(entry.client));
self.members.retain(|member| !covered(member.client));
self.native_disable.retain(|client, _| !covered(client));
if let Some(&first) = self.clients.first() {
self.target.client = first;
}
!self.clients.is_empty()
}
}
impl EntryPlan {
pub fn blocked_by_secret(&self) -> bool {
self.problem.as_ref().is_some_and(|err| {
matches!(
err.code(),
ErrorCode::MissingSecret
| ErrorCode::SecretLocked
| ErrorCode::ProviderUnavailable
| ErrorCode::ProviderTooOld
)
})
}
}
impl TargetPlan {
pub fn client(&self) -> &'static str {
self.clients[0]
}
pub fn counts(&self) -> OpCounts {
let mut counts = OpCounts::default();
for entry in &self.entries {
match entry.action {
Action::Create | Action::AdoptOverwrite => counts.create += 1,
Action::Update => counts.update += 1,
Action::Remove => counts.remove += 1,
Action::Disable => counts.disable += 1,
_ => {}
}
}
counts
}
pub fn has_work(&self) -> bool {
self.after.is_some()
|| self.entries.iter().any(|entry| {
matches!(
entry.action,
Action::Adopt
| Action::Refresh
| Action::Forget
| Action::Release
| Action::Import
)
})
}
pub fn unresolved(&self) -> usize {
self.entries.iter().filter(|entry| entry.unresolved).count()
}
pub fn is_blocked(&self) -> bool {
self.problem.is_some()
|| self
.entries
.iter()
.any(|entry| entry.state == EntryState::Blocked)
}
pub fn diff(&self) -> String {
let Some(after) = &self.after else {
return String::new();
};
let before = self.mask(&String::from_utf8_lossy(
self.before.as_deref().unwrap_or_default(),
));
let after = self.mask(&String::from_utf8_lossy(after));
let name = self.file.to_string_lossy();
let old = if self.before.is_some() {
format!("a/{name}")
} else {
"/dev/null".to_owned()
};
TextDiff::from_lines(before.as_str(), after.as_str())
.unified_diff()
.context_radius(3)
.header(&old, &format!("b/{name}"))
.to_string()
}
pub fn mask(&self, text: &str) -> String {
let mut text = text.to_owned();
let writes = self.input.desired.iter().flat_map(|entry| &entry.inline);
let values = writes
.map(|value| (value, "********"))
.chain(self.masked.iter().map(|value| (value, "******** (before)")));
for (value, placeholder) in values {
let raw = value.expose();
if raw.chars().count() < 4 {
continue;
}
let escaped = serde_json::to_string(raw).expect("a string serializes");
let escaped = &escaped[1..escaped.len() - 1];
for form in [escaped, raw] {
text = text.replace(form, placeholder);
}
}
text
}
pub fn snippet(&self, name: &str, value: &NativeValue) -> Option<String> {
let keys: Vec<&str> = self
.container
.last()
.map(String::as_str)
.into_iter()
.collect();
let mut doc = Document::empty(self.input.target.format);
doc.upsert(&keys, name, value).ok()?;
Some(self.mask(&String::from_utf8_lossy(&doc.to_bytes())))
}
}
fn old_inline_values(entry: &EntryPlan) -> Vec<SecretValue> {
let Some(observed) = &entry.observed else {
return Vec::new();
};
let mut kept = Vec::new();
if let Some(desired) = &entry.desired {
leaves(desired, false, &mut |text, _| kept.push(text.to_owned()));
}
let mut out = Vec::new();
leaves(observed, false, &mut |text, secret_map| {
if secret_map || !kept.iter().any(|k| k == text) {
out.push(SecretValue::new(text));
}
});
out
}
fn leaves(value: &NativeValue, secret_map: bool, f: &mut dyn FnMut(&str, bool)) {
match value {
NativeValue::String(text) => f(text, secret_map),
NativeValue::Array(items) => {
for item in items {
leaves(item, secret_map, f);
}
}
NativeValue::Object(map) => {
for (key, item) in map.iter() {
let secret =
secret_map || matches!(key, "env" | "headers" | "http_headers" | "environment");
leaves(item, secret, f);
}
}
_ => {}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct OpCounts {
pub create: u32,
pub update: u32,
pub remove: u32,
pub disable: u32,
}
impl OpCounts {
pub fn total(&self) -> u32 {
self.create + self.update + self.remove + self.disable
}
}
#[derive(Debug, Clone, Default)]
pub struct Plan {
pub targets: Vec<TargetPlan>,
pub warnings: Vec<CoreError>,
base: usize,
user: usize,
}
impl Plan {
pub fn writes(&self) -> usize {
self.targets
.iter()
.filter(|target| target.after.is_some())
.count()
}
pub fn unresolved(&self) -> usize {
self.targets.iter().map(TargetPlan::unresolved).sum()
}
pub fn has_work(&self) -> bool {
self.targets.iter().any(TargetPlan::has_work)
}
pub fn scope_part(&self, scope: ScopeFilter) -> Plan {
let (base, user) = (self.base, self.base + self.user);
let warnings: Vec<CoreError> = match scope {
ScopeFilter::All => self.warnings.clone(),
ScopeFilter::System => self.warnings[..user].to_vec(),
ScopeFilter::Project => self.warnings[..base]
.iter()
.chain(&self.warnings[user..])
.cloned()
.collect(),
};
Plan {
targets: self
.targets
.iter()
.filter(|target| match scope {
ScopeFilter::All => true,
ScopeFilter::System => target.scope == Scope::User,
ScopeFilter::Project => target.scope != Scope::User,
})
.cloned()
.collect(),
base: self.base,
user: if scope == ScopeFilter::Project {
0
} else {
self.user
},
warnings,
}
}
}
pub fn plan(
cx: &RenderCx<'_>,
desired: &DesiredState,
ledger: &Ledger,
key: &LedgerKey,
options: &PlanOptions,
) -> Result<Plan, CoreError> {
let mut plans = plan_each(cx, desired, ledger, key, std::slice::from_ref(options))?;
Ok(plans.pop().expect("one plan per selection"))
}
pub(crate) fn plan_each(
cx: &RenderCx<'_>,
desired: &DesiredState,
ledger: &Ledger,
key: &LedgerKey,
selections: &[PlanOptions],
) -> Result<Vec<Plan>, CoreError> {
let rendering = system_entries(cx, desired);
let mut files = Files::default();
selections
.iter()
.map(|options| plan_one(cx, desired, &rendering, ledger, key, options, &mut files))
.collect()
}
#[derive(Default)]
struct Files {
bytes: HashMap<PathBuf, Option<Vec<u8>>>,
docs: HashMap<(PathBuf, Format), Result<Document, CoreError>>,
access: HashMap<PathBuf, WriteAccess>,
}
impl Files {
#[allow(clippy::type_complexity)]
fn of(
&mut self,
fs: &dyn crate::fs::Fs,
target: &Target,
) -> Result<(Option<Vec<u8>>, Option<Result<Document, CoreError>>), CoreError> {
let file = &target.file;
let bytes = match self.bytes.get(file) {
Some(bytes) => bytes.clone(),
None => {
let bytes = read_target(fs, file)?;
self.bytes.insert(file.clone(), bytes.clone());
bytes
}
};
let parsed = bytes.as_ref().map(|bytes| {
self.docs
.entry((file.clone(), target.format))
.or_insert_with(|| Document::parse(bytes, target.format))
.clone()
});
Ok((bytes, parsed))
}
fn probe(&mut self, cx: &RenderCx<'_>, inputs: &[TargetInput]) -> Result<(), CoreError> {
let mut wanted: Vec<(PathBuf, Option<u64>)> = Vec::new();
for input in inputs {
let file = &input.target.file;
if self.access.contains_key(file) || wanted.iter().any(|(path, _)| path == file) {
continue;
}
let (bytes, _) = self.of(cx.fs, &input.target)?;
wanted.push((file.clone(), access::fingerprint(bytes.as_deref())));
}
if !wanted.is_empty() {
self.access
.extend(access::probe_files(cx.fs, &wanted, cx.probes));
}
Ok(())
}
}
fn plan_one(
cx: &RenderCx<'_>,
desired: &DesiredState,
rendering: &Rendering,
ledger: &Ledger,
key: &LedgerKey,
options: &PlanOptions,
files: &mut Files,
) -> Result<Plan, CoreError> {
let mut plan = Plan {
targets: Vec::new(),
warnings: rendering.warnings.clone(),
base: 0,
user: 0,
};
for (id, err) in &desired.invalid {
plan.warnings.push(CoreError::new(
err.code(),
format!("server {id:?} is not applied, and entries it owns are left alone: {err}"),
));
}
plan.base = plan.warnings.len();
let mut inputs: Vec<TargetInput> = Vec::new();
if options.plans_user() {
user_inputs(
cx,
desired,
rendering,
ledger,
options,
&mut plan,
&mut inputs,
);
}
plan.user = plan.warnings.len() - plan.base;
let mut derived = Vec::new();
if options.scope.project() {
let mut planner = ProjectPlanner {
cx,
desired,
rendering,
ledger,
options,
detected: Detected::default(),
warnings: Vec::new(),
inputs: Vec::new(),
derived: Vec::new(),
};
planner.plan_projects();
plan.warnings.append(&mut planner.warnings);
inputs.append(&mut planner.inputs);
derived = planner.derived;
}
inputs.retain(|input| {
options.selects_target(&input.target) && options.selects_servers(input, ledger, &derived)
});
if let Some(ejected) = crate::eject::status_at(cx.fs, &cx.surjection_home.join(STATE_DIR))? {
inputs.retain_mut(|input| input.without_ejected(&ejected));
}
files.probe(cx, &inputs)?;
for mut input in inputs {
let (bytes, parsed) = files.of(cx.fs, &input.target)?;
input.access = files
.access
.get(&input.target.file)
.copied()
.unwrap_or(WriteAccess::Unknown);
plan.targets.push(plan_target_in(
cx, input, bytes, parsed, ledger, key, options,
));
}
Ok(plan)
}
fn user_inputs(
cx: &RenderCx<'_>,
desired: &DesiredState,
rendering: &Rendering,
ledger: &Ledger,
options: &PlanOptions,
plan: &mut Plan,
inputs: &mut Vec<TargetInput>,
) {
let owned: Vec<&str> = ledger
.entries
.iter()
.filter(|entry| entry.kind == EntryKind::McpEntry && entry.scope == Scope::User)
.map(|entry| entry.client.as_str())
.collect();
let clients: Vec<&'static AdapterSpec> = ADAPTERS
.iter()
.copied()
.filter(|spec| rendering.clients.contains(&spec.id) || owned.contains(&spec.id))
.filter(|spec| options.selects(spec.id))
.collect();
for spec in clients {
let prefs = desired.prefs(spec.id);
let targets = match spec.mcp_targets(cx.env, cx.fs, Scope::User, None, &prefs) {
Ok(targets) => targets,
Err(err) => {
plan.warnings.push(err);
continue;
}
};
let desired_here: Vec<DesiredEntry> = rendering
.entries
.iter()
.filter(|entry| entry.client == spec.id)
.cloned()
.collect();
let native_disable = prefs.native_disable == Some(true);
let extension = replacing_extension(spec, cx.env, cx.fs, None);
for mut target in targets {
target.warnings.extend(extension.clone());
match inputs
.iter_mut()
.find(|input| input.same(&target, EntryKind::McpEntry))
{
Some(input) => {
input.join(spec.id, native_disable, target.warnings);
input.desired.extend(desired_here.iter().cloned());
}
None => {
let mut input = TargetInput::new(target, spec.id, native_disable);
input.desired = desired_here.clone();
input.invalid_servers = desired.invalid.keys().cloned().collect();
inputs.push(input);
}
}
}
}
}
struct ProjectPlanner<'a, 'c> {
cx: &'a RenderCx<'c>,
desired: &'a DesiredState,
rendering: &'a Rendering,
ledger: &'a Ledger,
options: &'a PlanOptions,
detected: Detected,
warnings: Vec<CoreError>,
inputs: Vec<TargetInput>,
derived: Vec<(String, String)>,
}
struct ProjectCx<'p> {
id: &'p str,
root: &'p Path,
blocked: Option<CoreError>,
}
impl ProjectPlanner<'_, '_> {
fn plan_projects(&mut self) {
let state_dir = self.cx.surjection_home.join(STATE_DIR);
let trust = TrustStore::load(self.cx.fs, &state_dir).unwrap_or_else(|err| {
self.warnings.push(err);
TrustStore::default()
});
let known = layering::known_projects(
self.cx.fs,
self.cx.env,
self.desired.overlay.as_ref(),
&trust,
);
if let ProjectSelection::Only(root) = &self.options.projects {
match known
.iter()
.find(|project| layering::same_path(&project.root, root))
{
None => self.warnings.push(
CoreError::new(
ErrorCode::NotFound,
"this folder is not a project known on this machine",
)
.with_path(root),
),
Some(project) if matches!(project.status, ProjectStatus::Missing) => {
self.warnings.push(
CoreError::new(ErrorCode::NotFound, "the project folder does not exist")
.with_path(root),
);
}
Some(_) => {}
}
}
let options = self.options;
for project in known
.iter()
.filter(|project| options.plans_project(project))
{
match &project.status {
ProjectStatus::Missing => {}
ProjectStatus::Failed(err) => self.warnings.push(err.clone()),
ProjectStatus::Loaded {
project: loaded, ..
} => {
self.warnings.extend(loaded.warnings.iter().cloned());
let pcx = ProjectCx {
id: &project.id,
root: &project.root,
blocked: project.trust_error(),
};
let start = self.inputs.len();
let mut held = self.plan_project(&pcx, loaded);
held.extend(self.desired.invalid.keys().cloned());
for input in &mut self.inputs[start..] {
input.project_id = Some(project.id.clone());
input.blocked.clone_from(&pcx.blocked);
input.invalid_servers.extend(held.iter().cloned());
}
self.cleanup(
|id| id == project.id,
Some(&project.root),
pcx.blocked.as_ref(),
&held,
);
}
}
}
if self.options.projects == ProjectSelection::All {
let known_ids: Vec<&str> = known.iter().map(|project| project.id.as_str()).collect();
let invalid: Vec<String> = self.desired.invalid.keys().cloned().collect();
self.cleanup(|id| !known_ids.contains(&id), None, None, &invalid);
}
}
fn plan_project(&mut self, pcx: &ProjectCx<'_>, project: &Project) -> Vec<String> {
let entries = layering::project_entries(
project,
&self.desired.servers,
self.cx.env,
self.desired.overlay.as_ref(),
);
let mut warned: Vec<String> = Vec::new();
let mut held = Vec::new();
for entry in &entries {
let from_selected = [Some(entry), entry.shared.as_deref()]
.into_iter()
.flatten()
.filter_map(|entry| entry.from.as_deref())
.any(|from| {
self.options
.servers
.as_ref()
.is_some_and(|ids| ids.iter().any(|id| id == from))
});
if from_selected {
self.derived.push((pcx.id.to_owned(), entry.name.clone()));
}
if matches!(entry.body, EntryBody::Server(Err(_))) {
held.push(entry.name.clone());
}
self.plan_entry(pcx, entry, false, &mut warned);
if let Some(shared) = entry
.shared
.as_deref()
.filter(|shared| matches!(shared.body, EntryBody::Server(_)))
{
self.plan_entry(pcx, shared, true, &mut warned);
}
}
held
}
fn plan_entry(
&mut self,
pcx: &ProjectCx<'_>,
entry: &ProjectEntry,
private_only: bool,
warned: &mut Vec<String>,
) {
let clients = match &entry.body {
EntryBody::Server(Err(_)) => self.error_clients(pcx, entry),
EntryBody::Server(Ok(_)) | EntryBody::Suppression => {
self.detected
.project_selection(self.cx, self.desired, entry)
}
};
for client in clients {
if !self.options.selects(&client) || self.desired.excluded(&client) {
continue;
}
let spec = match writable(&client, self.cx.env) {
Ok(spec) => spec,
Err(err) => {
if !warned.contains(&client) {
warned.push(client);
self.warnings.push(err);
}
continue;
}
};
if private_only
&& spec
.mcp
.as_ref()
.is_none_or(|mcp| mcp.project_local.is_empty())
{
continue;
}
match &entry.body {
EntryBody::Server(server) => self.server_entry(pcx, entry, server, spec),
EntryBody::Suppression => self.suppression(pcx, entry, spec),
}
}
}
fn error_clients(&mut self, pcx: &ProjectCx<'_>, entry: &ProjectEntry) -> Vec<String> {
let mut clients = match &entry.clients {
ClientSelection::List(ids) => ids.clone(),
ClientSelection::AllDetected => {
let mut ids = Vec::new();
for spec in ADAPTERS.iter().copied() {
let has_files = spec.mcp.as_ref().is_some_and(|mcp| {
!mcp.read_only
&& mcp.oses.contains(&self.cx.env.os())
&& !(mcp.project.is_empty() && mcp.project_local.is_empty())
});
if has_files && self.detected.detected(self.cx, self.desired, spec) {
ids.push(spec.id.to_owned());
}
}
ids
}
};
for row in &self.ledger.entries {
let owns = row.kind == EntryKind::McpEntry
&& row.project_id.as_deref() == Some(pcx.id)
&& (row.server_id.as_deref() == Some(entry.name.as_str()) || row.key == entry.name);
if owns && !clients.contains(&row.client) {
clients.push(row.client.clone());
}
}
clients
}
fn placement(
&mut self,
pcx: &ProjectCx<'_>,
spec: &'static AdapterSpec,
mcp: &McpSpec,
layer: EntryLayer,
) -> Option<(Target, Sharing)> {
let (scope, sharing) = match layer {
EntryLayer::Shared => (Scope::Project, Sharing::Shared),
EntryLayer::Local if mcp.project_local.is_empty() => (Scope::Project, Sharing::Private),
EntryLayer::Local => (Scope::ProjectLocal, Sharing::Private),
};
let prefs = self.desired.prefs(spec.id);
match spec.mcp_targets(self.cx.env, self.cx.fs, scope, Some(pcx.root), &prefs) {
Ok(mut targets) if !targets.is_empty() => {
let mut target = targets.swap_remove(0);
target.warnings.extend(replacing_extension(
spec,
self.cx.env,
self.cx.fs,
Some(pcx.root),
));
Some((target, sharing))
}
Ok(_) => None,
Err(err) => {
self.warnings.push(err);
None
}
}
}
fn private_file(
fs: &dyn crate::fs::Fs,
pcx: &ProjectCx<'_>,
spec: &AdapterSpec,
target: &Target,
) -> Result<bool, CoreError> {
if target.scope != Scope::Project {
return Ok(false);
}
fs.note_dependency(&pcx.root.join(".git"));
let Some(repo) = ProjectRepo::discover(pcx.root)? else {
return Ok(false);
};
for file in repo.state_files(&target.file) {
fs.note_dependency(&file);
}
if repo.is_tracked(&target.file)? {
let file = repo
.relative(&target.file)
.unwrap_or_else(|| target.file.to_string_lossy().into_owned());
return Err(CoreError::new(
ErrorCode::PrivateInTrackedFile,
format!(
"a project.local.toml entry would go to {file}, which git tracks; remove {} \
from this entry's clients, or untrack {file}",
spec.id
),
)
.with_path(&target.file));
}
Ok(!repo.is_ignored(&target.file)?)
}
fn server_entry(
&mut self,
pcx: &ProjectCx<'_>,
entry: &ProjectEntry,
server: &Result<Box<Server>, CoreError>,
spec: &'static AdapterSpec,
) {
let mcp = spec
.mcp
.as_ref()
.expect("writable clients have MCP support");
if mcp.project.is_empty() && mcp.project_local.is_empty() {
return;
}
let Some((target, sharing)) = self.placement(pcx, spec, mcp, entry.layer) else {
return;
};
let mut exclude = false;
let mut want = DesiredEntry {
client: spec.id,
server_id: entry.name.clone(),
name: server.as_ref().map_or_else(
|_| entry.name.clone(),
|server| native_name(server, spec.id),
),
server: Err(CoreError::new(ErrorCode::InvalidValue, "not rendered")),
delivery: None,
inline: Vec::new(),
warnings: Vec::new(),
native: None,
};
let ready = match (&pcx.blocked, server) {
(Some(err), _) | (None, Err(err)) => Err(err.clone()),
(None, Ok(server)) => match entry.layer {
EntryLayer::Local => {
Self::private_file(self.cx.fs, pcx, spec, &target).map(|hide| {
exclude = hide;
server
})
}
EntryLayer::Shared => Ok(server),
},
};
want.server = ready.and_then(|server| {
let out = render_for(
self.cx,
self.desired,
server,
spec,
RenderTarget {
sharing,
project: Some(pcx.root),
overlay: false,
},
)?;
want.delivery = out.delivery.map(|mode| mode.as_str());
want.inline = out.inline;
want.warnings = out.warnings;
let mut rendered = out.server;
rendered.enabled = entry.enabled;
Ok(rendered)
});
if want.server.is_ok() && mcp.caps.project_semantics == ProjectSemantics::DeepMerge {
want.warnings
.extend(unset_not_expressible(spec, entry, &target.file));
}
let native_disable = self.desired.prefs(spec.id).native_disable == Some(true);
let input = self.input_for(target, spec.id, native_disable, EntryKind::McpEntry);
input.exclude |= exclude;
input.desired.push(want);
input.sources.extend(entry.from.clone());
}
fn suppression(
&mut self,
pcx: &ProjectCx<'_>,
entry: &ProjectEntry,
spec: &'static AdapterSpec,
) {
let Some(server) = self.desired.servers.get(&entry.name) else {
return;
};
let Some(system) = self
.rendering
.entries
.iter()
.find(|want| want.client == spec.id && want.server_id == entry.name)
else {
return;
};
let mechanism = match layering::suppression_mechanism(spec, &entry.name) {
Ok(mechanism) => mechanism,
Err(warning) => {
self.warnings.push(warning);
return;
}
};
let mcp = spec
.mcp
.as_ref()
.expect("writable clients have MCP support");
let native_disable = self.desired.prefs(spec.id).native_disable == Some(true);
let name = native_name(server, spec.id);
match mechanism {
ProjectDisable::ListMember(_) => {
let prefs = self.desired.prefs(spec.id);
let target =
match spec.suppression_target(self.cx.env, self.cx.fs, pcx.root, &prefs) {
Ok(Some(target)) => target,
Ok(None) => return,
Err(err) => {
self.warnings.push(err);
return;
}
};
let mut exclude = false;
if pcx.blocked.is_none() && matches!(entry.layer, EntryLayer::Local) {
match Self::private_file(self.cx.fs, pcx, spec, &target) {
Ok(hide) => exclude = hide,
Err(err) => {
self.warnings.push(err);
return;
}
}
}
let input = self.input_for(target, spec.id, native_disable, EntryKind::ListMember);
input.exclude |= exclude;
input.members.push(DesiredMember {
client: spec.id,
server_id: entry.name.clone(),
name,
});
}
ProjectDisable::EnabledFalse { partial } => {
let Some((target, sharing)) = self.placement(pcx, spec, mcp, entry.layer) else {
return;
};
let mut exclude = false;
let mut want = DesiredEntry {
client: spec.id,
server_id: entry.name.clone(),
name,
server: Err(CoreError::new(ErrorCode::InvalidValue, "not rendered")),
delivery: None,
inline: Vec::new(),
warnings: Vec::new(),
native: None,
};
let ready = match (&pcx.blocked, entry.layer) {
(Some(err), _) => Err(err.clone()),
(None, EntryLayer::Local) => Self::private_file(self.cx.fs, pcx, spec, &target)
.map(|hide| exclude = hide),
(None, EntryLayer::Shared) => Ok(()),
};
want.server = ready.and_then(|()| match (&system.server, partial) {
(Ok(rendered), true) => Ok(rendered.clone()),
_ => {
let out = render_for(
self.cx,
self.desired,
server,
spec,
RenderTarget {
sharing,
project: Some(pcx.root),
overlay: true,
},
)?;
want.delivery = out.delivery.map(|mode| mode.as_str());
want.inline = out.inline;
want.warnings = out.warnings;
Ok(out.server)
}
});
if let Ok(rendered) = &want.server {
match mcp.suppression_entry(rendered, self.cx.env.os()) {
Ok(native) => want.native = native,
Err(err) => want.server = Err(err.into()),
}
}
if partial {
want.delivery = None;
want.inline.clear();
}
let input = self.input_for(target, spec.id, native_disable, EntryKind::McpEntry);
input.exclude |= exclude;
input.desired.push(want);
}
}
}
fn input_for(
&mut self,
target: Target,
client: &'static str,
native_disable: bool,
kind: EntryKind,
) -> &mut TargetInput {
let index = match self
.inputs
.iter()
.position(|input| input.same(&target, kind))
{
Some(index) => {
self.inputs[index].join(client, native_disable, target.warnings);
index
}
None => {
let mut input = TargetInput::new(target, client, native_disable);
input.kind = kind;
self.inputs.push(input);
self.inputs.len() - 1
}
};
&mut self.inputs[index]
}
fn cleanup(
&mut self,
project: impl Fn(&str) -> bool,
root: Option<&Path>,
blocked: Option<&CoreError>,
held: &[String],
) {
type Covered = (Scope, Option<String>, String, String, EntryKind);
let covered: Vec<Covered> = self
.inputs
.iter()
.map(|input| {
let pointer = input
.container_keys()
.map(|keys| container_pointer(&keys))
.unwrap_or_default();
(
input.target.scope,
input.project_id.clone(),
input.target.file.to_string_lossy().into_owned(),
pointer,
input.kind,
)
})
.collect();
let mut orphans: Vec<TargetInput> = Vec::new();
for row in &self.ledger.entries {
let Some(id) = row.project_id.as_deref() else {
continue;
};
if row.scope == Scope::User || !project(id) {
continue;
}
let Some(spec) = adapter(&row.client) else {
continue;
};
let Some(mcp) = spec.mcp.as_ref() else {
continue;
};
if !self.options.selects(spec.id) {
continue;
}
let native_disable = self.desired.prefs(spec.id).native_disable == Some(true);
let covering = covered
.iter()
.position(|(scope, project_id, file, pointer, kind)| {
*scope == row.scope
&& project_id.as_deref() == Some(id)
&& *file == row.file
&& !pointer.is_empty()
&& *pointer == row.container
&& *kind == row.kind
});
if let Some(index) = covering {
self.inputs[index].join(spec.id, native_disable, Vec::new());
continue;
}
let same = orphans.iter_mut().find(|input| {
input.target.scope == row.scope
&& input.project_id.as_deref() == Some(id)
&& input.target.file.to_string_lossy() == row.file
&& input.keys.as_deref().map(container_pointer).as_deref()
== Some(row.container.as_str())
&& input.kind == row.kind
});
if let Some(input) = same {
input.join(spec.id, native_disable, Vec::new());
continue;
}
let target = Target {
client: spec.id,
scope: row.scope,
os: self.cx.env.os(),
file: PathBuf::from(&row.file),
format: mcp.format,
container: match row.scope {
Scope::ProjectLocal => mcp.project_local_container,
_ => mcp.container,
},
project: root.map(Path::to_path_buf),
profile: None,
skeleton: mcp.skeleton,
warnings: Vec::new(),
};
let mut input = TargetInput::new(target, spec.id, native_disable);
input.kind = row.kind;
input.keys = Some(pointer_keys(&row.container));
input.project_id = Some(id.to_owned());
input.blocked = blocked.cloned();
input.invalid_servers = held.to_vec();
orphans.push(input);
}
self.inputs.append(&mut orphans);
}
}
pub(crate) fn unset_not_expressible(
spec: &AdapterSpec,
entry: &ProjectEntry,
file: &Path,
) -> Option<CoreError> {
if entry.unset.is_empty() {
return None;
}
Some(
CoreError::new(
ErrorCode::UnsetNotExpressible,
format!(
"{} merges project entries over user entries field by field, so `unset` ({}) \
of {:?} cannot remove what the user entry sets; the entry is written anyway",
spec.display_name,
entry.unset.join(", "),
entry.name
),
)
.with_path(file),
)
}
pub(crate) fn client_enable_required(
spec: &AdapterSpec,
names: &[&str],
file: &Path,
) -> Option<CoreError> {
let TrustGate::ClientEnable(place) = spec.mcp.as_ref()?.caps.trust_gate? else {
return None;
};
if names.is_empty() {
return None;
}
let names: Vec<String> = names.iter().map(|name| format!("{name:?}")).collect();
Some(
CoreError::new(
ErrorCode::ClientEnableRequired,
format!(
"{} lists new servers of this project file as Disabled until each is enabled \
in {place} (once per server; Surjection cannot read that toggle): {}",
spec.display_name,
names.join(", ")
),
)
.with_path(file),
)
}
fn client_enable_hints(plan: &TargetPlan) -> Vec<CoreError> {
if plan.scope == Scope::User {
return Vec::new();
}
plan.clients
.iter()
.filter_map(|&client| {
let names: Vec<&str> = plan
.entries
.iter()
.filter(|entry| {
entry.client == Some(client)
&& entry.desired.is_some()
&& entry.state != EntryState::Blocked
&& !matches!(
entry.action,
Action::Skip | Action::Import | Action::Release
)
})
.map(|entry| entry.name.as_str())
.collect();
client_enable_required(adapter(client)?, &names, &plan.file)
})
.collect()
}
fn pointer_keys(pointer: &str) -> Vec<String> {
pointer
.split('/')
.skip(1)
.map(|key| key.replace("~1", "/").replace("~0", "~"))
.collect()
}
fn same_folder(os: Os, a: &str, b: &str) -> bool {
let windows = os == Os::Windows;
normalize_path_text(windows, a) == normalize_path_text(windows, b)
}
pub(crate) fn read_target(
fs: &dyn crate::fs::Fs,
file: &std::path::Path,
) -> Result<Option<Vec<u8>>, CoreError> {
if !fs.exists(file) {
return Ok(None);
}
fs.read(file).map(Some).map_err(|err| {
CoreError::new(ErrorCode::IoError, format!("cannot read file: {err}")).with_path(file)
})
}
fn mapper_of(client: &str) -> Option<&'static dyn EntryMapper> {
adapter(client)
.and_then(|spec| spec.mcp.as_ref())
.map(|mcp| mcp.mapper)
}
pub(crate) fn plan_target(
cx: &RenderCx<'_>,
input: TargetInput,
bytes: Option<Vec<u8>>,
ledger: &Ledger,
key: &LedgerKey,
options: &PlanOptions,
) -> TargetPlan {
plan_target_in(cx, input, bytes, None, ledger, key, options)
}
fn plan_target_in(
cx: &RenderCx<'_>,
input: TargetInput,
bytes: Option<Vec<u8>>,
parsed: Option<Result<Document, CoreError>>,
ledger: &Ledger,
key: &LedgerKey,
options: &PlanOptions,
) -> TargetPlan {
read_only_blocks(plan_target_body(
cx, input, bytes, parsed, ledger, key, options,
))
}
fn read_only_blocks(mut plan: TargetPlan) -> TargetPlan {
let Some(kind) = plan.read_only else {
return plan;
};
if plan.problem.is_some() {
return plan;
}
let err = access::read_only_target(kind, &plan.file);
for entry in &mut plan.entries {
if entry.action.writes() {
entry.state = EntryState::Blocked;
entry.action = Action::None;
entry.unresolved = false;
entry.problem = Some(err.clone());
}
}
plan.after = None;
plan
}
fn plan_target_body(
cx: &RenderCx<'_>,
input: TargetInput,
bytes: Option<Vec<u8>>,
parsed: Option<Result<Document, CoreError>>,
ledger: &Ledger,
key: &LedgerKey,
options: &PlanOptions,
) -> TargetPlan {
let target = &input.target;
let mut plan = TargetPlan {
clients: input.clients.clone(),
scope: target.scope,
project_id: input.project_id.clone(),
project: target.project.clone(),
kind: input.kind,
file: target.file.clone(),
container: Vec::new(),
pointer: String::new(),
before: bytes.clone(),
after: None,
entries: Vec::new(),
problem: None,
warnings: target
.warnings
.iter()
.chain(input.desired.iter().flat_map(|entry| &entry.warnings))
.cloned()
.collect(),
masked: Vec::new(),
read_only: input.access.read_only(),
input: input.clone(),
};
let keys = match input.container_keys() {
Ok(keys) => keys,
Err(err) => return blocked_target(plan, err.with_path(&target.file)),
};
plan.pointer = container_pointer(&keys);
plan.container = keys.clone();
if let Some(err) = &input.blocked {
return blocked_target(plan, err.clone());
}
let parsed = match (&bytes, parsed) {
(Some(_), Some(parsed)) => parsed,
(Some(bytes), None) => Document::parse(bytes, target.format),
(None, _) => Ok(target.new_document()),
};
let mut doc = match parsed {
Ok(doc) => doc,
Err(err) => return blocked_target(plan, err.with_path(&target.file)),
};
if let Some(warning) = key_spelling(target, &doc, &keys) {
plan.warnings.push(warning);
}
if input.kind == EntryKind::ListMember {
return plan_list(plan, doc, bytes, ledger, options);
}
let container: Vec<&str> = keys.iter().map(String::as_str).collect();
let observed_names = match doc.list(&container) {
Ok(names) => names,
Err(err) => return blocked_target(plan, err.with_path(&target.file)),
};
let file = target.file.to_string_lossy().into_owned();
let (pointer, project_id) = (plan.pointer.clone(), plan.project_id.clone());
let slot = LedgerSlot {
scope: plan.scope,
project_id: project_id.as_deref(),
file: &file,
container: &pointer,
};
let owned_here = ledger.slot_entries(&slot);
let observed_set: HashSet<&str> = observed_names.iter().map(String::as_str).collect();
let mut names: Vec<String> = observed_names.clone();
let mut listed: HashSet<String> = observed_names.iter().cloned().collect();
for entry in &input.desired {
if listed.insert(entry.name.clone()) {
names.push(entry.name.clone());
}
}
for entry in owned_here.iter() {
if entry.kind == EntryKind::McpEntry && listed.insert(entry.key.clone()) {
names.push(entry.key.clone());
}
}
let foreign_names: Vec<String> = observed_names
.iter()
.filter(|name| owned_here.get(name).is_none())
.cloned()
.collect();
let readers = OnceCell::new();
for name in names {
let observed = if observed_set.contains(name.as_str()) {
match doc.get(&container, &name) {
Ok(value) => value,
Err(err) => {
plan.entries.push(blocked_entry(&name, None, None, err));
continue;
}
}
} else {
None
};
let owned = owned_here
.get(&name)
.filter(|entry| entry.kind == EntryKind::McpEntry);
let wanted: Vec<&DesiredEntry> = input
.desired
.iter()
.filter(|entry| entry.name == name)
.collect();
let entry = plan_entry(
cx,
&input,
&name,
&foreign_names,
wanted,
observed,
owned,
key,
options,
&readers,
);
if let Some(mut entry) = entry {
if !options.selects_entry(&input.target, &pointer, &entry.name) {
entry.action = Action::None;
entry.written = None;
entry.unresolved = false;
}
let was_inline = owned.and_then(|l| l.delivery.as_deref()) == Some("inline");
if was_inline || entry.delivery == Some("inline") {
plan.masked.extend(old_inline_values(&entry));
}
plan.entries.push(entry);
}
}
let mut changed = false;
for entry in &plan.entries {
let result = match entry.action {
Action::Create | Action::Update | Action::AdoptOverwrite | Action::Disable => {
let value = entry.written.as_ref().expect("write actions carry a value");
doc.upsert(&container, &entry.name, value)
}
Action::Remove => doc.remove(&container, &entry.name),
_ => Ok(false),
};
match result {
Ok(edited) => changed |= edited,
Err(err) => return blocked_target(plan, err.with_path(&target.file)),
}
}
if changed {
let after = doc.to_bytes();
if bytes.as_deref() != Some(after.as_slice()) {
plan.after = Some(after);
}
}
plan.warnings.extend(client_enable_hints(&plan));
plan
}
type TimeoutReaders = Vec<(&'static str, &'static [TimeoutField])>;
fn timeout_unit_conflict(
env: &Env,
target: &Target,
native: &NativeValue,
readers: &OnceCell<TimeoutReaders>,
) -> Option<CoreError> {
let entry = native.as_object()?;
if target.scope != Scope::User {
return None;
}
let readers = readers.get_or_init(|| {
ADAPTERS
.iter()
.filter_map(|spec| {
let mcp = spec.mcp.as_ref()?;
let reads = mcp.oses.contains(&env.os())
&& mcp
.user
.iter()
.filter_map(|location| location.resolve(env, None))
.any(|file| layering::same_path(&file, &target.file));
reads.then_some((spec.id, mcp.caps.timeouts))
})
.collect()
});
let mut keys = readers
.iter()
.flat_map(|(_, timeouts)| timeouts.iter())
.map(|timeout| timeout.key)
.filter(|key| entry.contains_key(key));
keys.find_map(|key| {
let units: Vec<(&str, TimeUnit)> = readers
.iter()
.filter_map(|(client, timeouts)| {
let field = timeouts.iter().find(|timeout| timeout.key == key)?;
Some((*client, field.unit))
})
.collect();
if units.iter().all(|(_, unit)| *unit == units[0].1) {
return None;
}
let clients: Vec<&str> = units.iter().map(|(client, _)| *client).collect();
let described: Vec<String> = units
.iter()
.map(|(client, unit)| format!("{client} in {}", unit.name()))
.collect();
Some(
CoreError::new(
ErrorCode::TimeoutUnitConflict,
format!(
"clients {} read this file and take `{key}` in different units ({}); \
remove `{key}` from this server's `[client.<id>]` tables, or give the \
clients separate files",
clients.join(" and "),
described.join(", ")
),
)
.with_path(&target.file),
)
})
}
#[allow(clippy::too_many_arguments)]
fn plan_entry(
cx: &RenderCx<'_>,
input: &TargetInput,
name: &str,
foreign_names: &[String],
wanted: Vec<&DesiredEntry>,
observed: Option<NativeValue>,
owned: Option<&LedgerEntry>,
key: &LedgerKey,
options: &PlanOptions,
readers: &OnceCell<TimeoutReaders>,
) -> Option<EntryPlan> {
let os = cx.env.os();
let mut entry = EntryPlan {
name: name.to_owned(),
client: None,
server_id: None,
state: EntryState::Foreign,
action: Action::None,
unresolved: false,
problem: None,
desired: None,
observed: observed.clone(),
written: None,
delivery: None,
};
let mut desired: Option<(&'static str, NativeValue)> = None;
if let Some(first) = wanted.first() {
entry.client = Some(first.client);
entry.server_id = Some(first.server_id.clone());
entry.delivery = first.delivery;
let mut natives = Vec::new();
for want in &wanted {
let native = match (&want.server, &want.native) {
(Ok(_), Some(native)) => Ok(native.clone()),
(Ok(server), None) => mapper_of(want.client)
.ok_or_else(|| CoreError::new(ErrorCode::NoAdapter, "no mapper"))
.and_then(|mapper| {
mapper
.to_native(
server,
&WriteCx {
os,
existing: observed.as_ref(),
},
)
.map_err(CoreError::from)
}),
(Err(err), _) => Err(err.clone()),
};
match native {
Ok(native) => natives.push((want.client, native)),
Err(err) => {
entry.state = EntryState::Blocked;
entry.problem = Some(err);
return Some(entry);
}
}
}
if let Some(problem) = natives
.iter()
.find_map(|(_, native)| timeout_unit_conflict(cx.env, &input.target, native, readers))
{
entry.state = EntryState::Blocked;
entry.problem = Some(problem);
return Some(entry);
}
let (client, native) = natives.remove(0);
if natives.iter().any(|(_, other)| *other != native) {
entry.state = EntryState::Blocked;
entry.problem = Some(CoreError::new(
ErrorCode::Collision,
format!(
"clients {} share this file and want different entries named {name:?}",
wanted
.iter()
.map(|w| w.client)
.collect::<Vec<_>>()
.join(", ")
),
));
return Some(entry);
}
let native = match &observed {
Some(observed)
if input.target.scope == Scope::Project
&& crate::migrate::loadout::legacy_bare_launcher(observed, &native) =>
{
observed.clone()
}
_ => native,
};
entry.desired = Some(native.clone());
desired = Some((client, native));
}
let resolution = |entry: &EntryPlan| options.resolution(entry.client, name);
match (desired, observed, owned) {
(Some((client, d)), None, None) => {
let collisions = adapter(client)
.map(|spec| spec.name_collisions(name, foreign_names))
.unwrap_or_default();
if let Some(collision) = collisions.into_iter().next() {
entry.state = EntryState::Collision;
entry.action = Action::Skip;
entry.unresolved = resolution(&entry) == OnConflict::Ask;
entry.problem = Some(collision);
} else {
entry.state = EntryState::PendingCreate;
entry.action = Action::Create;
entry.written = Some(d);
}
}
(Some((_, d)), None, Some(_)) => {
entry.state = EntryState::Missing;
match resolution(&entry) {
OnConflict::Skip | OnConflict::Release => entry.action = Action::Skip,
OnConflict::Import => entry.action = Action::Import,
OnConflict::Ask | OnConflict::Overwrite => {
entry.action = Action::Create;
entry.written = Some(d);
}
}
}
(Some((client, d)), Some(o), None) => {
let mapper = mapper_of(client)?;
if mapper.canonical(&o) == mapper.canonical(&d) {
entry.state = EntryState::Synced;
entry.action = Action::Adopt;
entry.written = Some(o);
} else {
entry.state = EntryState::Collision;
let resolved = resolution(&entry);
decide(&mut entry, resolved, Action::AdoptOverwrite, d, false);
}
}
(Some((client, d)), Some(o), Some(l)) => {
let mapper = mapper_of(client)?;
let (canon_o, canon_d) = (mapper.canonical(&o), mapper.canonical(&d));
let hash_o = key.hash(&canon_o);
if Some(&hash_o) == l.written_hash.as_ref() {
if canon_o == canon_d {
entry.state = EntryState::Synced;
} else {
entry.state = EntryState::PendingUpdate;
entry.action = Action::Update;
entry.written = Some(d);
}
} else if canon_o == canon_d {
entry.state = EntryState::Synced;
entry.action = Action::Refresh;
entry.written = Some(o);
} else {
entry.state = if l.written_hash.as_ref() == Some(&key.hash(&canon_d)) {
EntryState::Drifted
} else {
EntryState::Conflict
};
let resolved = resolution(&entry);
decide(&mut entry, resolved, Action::Update, d, true);
}
}
(None, Some(o), Some(l)) => {
entry.client = adapter(&l.client).map(|spec| spec.id);
entry.server_id = l.server_id.clone();
let client = entry.client?;
if !input.clients.contains(&client) {
return None;
}
if l.server_id
.as_ref()
.is_some_and(|id| input.invalid_servers.contains(id))
{
entry.state = EntryState::Blocked;
entry.problem = Some(not_loading(l.server_id.as_deref()));
return Some(entry);
}
let mapper = mapper_of(client)?;
let canon_o = mapper.canonical(&o);
if Some(&key.hash(&canon_o)) == l.written_hash.as_ref() {
let disabled = input
.native_disable
.get(client)
.copied()
.unwrap_or(false)
.then(|| disabled_value(client, &o))
.flatten();
match disabled {
Some(value) if mapper.canonical(&value) == canon_o => {
entry.state = EntryState::Synced;
}
Some(value) => {
entry.state = EntryState::PendingRemove;
entry.action = Action::Disable;
entry.written = Some(value);
}
None => {
entry.state = EntryState::PendingRemove;
entry.action = Action::Remove;
}
}
} else {
entry.state = EntryState::Drifted;
match resolution(&entry) {
OnConflict::Overwrite => entry.action = Action::Remove,
OnConflict::Import | OnConflict::Release => entry.action = Action::Release,
OnConflict::Skip => entry.action = Action::Skip,
OnConflict::Ask => {
entry.action = Action::Skip;
entry.unresolved = true;
}
}
}
}
(None, None, Some(l)) => {
entry.client = adapter(&l.client).map(|spec| spec.id);
entry.server_id = l.server_id.clone();
if !entry
.client
.is_some_and(|client| input.clients.contains(&client))
{
return None;
}
entry.state = EntryState::Synced;
entry.action = Action::Forget;
}
(None, Some(_), None) => {
entry.state = EntryState::Foreign;
}
(None, None, None) => return None,
}
Some(entry)
}
fn decide(
entry: &mut EntryPlan,
resolution: OnConflict,
overwrite: Action,
desired: NativeValue,
owned: bool,
) {
match resolution {
OnConflict::Overwrite => {
entry.action = overwrite;
entry.written = Some(desired);
}
OnConflict::Import => entry.action = Action::Import,
OnConflict::Release if owned => entry.action = Action::Release,
OnConflict::Skip | OnConflict::Release => entry.action = Action::Skip,
OnConflict::Ask => {
entry.action = Action::Skip;
entry.unresolved = true;
}
}
}
fn disabled_value(client: &str, value: &NativeValue) -> Option<NativeValue> {
let mech = adapter(client)?.mcp.as_ref()?.caps.native_disable?;
let mut entry = value.as_object()?.clone();
match mech {
DisableMech::EnabledFalse => {
entry.insert("enabled", NativeValue::Bool(false));
}
DisableMech::DisabledTrue => {
entry.insert("disabled", NativeValue::Bool(true));
}
DisableMech::ListMember(_) => return None,
}
Some(NativeValue::Object(entry))
}
fn plan_list(
mut plan: TargetPlan,
mut doc: Document,
bytes: Option<Vec<u8>>,
ledger: &Ledger,
options: &PlanOptions,
) -> TargetPlan {
let keys = plan.container.clone();
let list: Vec<&str> = keys.iter().map(String::as_str).collect();
let observed = match list_members(&doc, &list) {
Ok(members) => members,
Err(err) => {
let err = err.with_path(&plan.file);
return blocked_target(plan, err);
}
};
let file = plan.file.to_string_lossy().into_owned();
let (pointer, project_id) = (plan.pointer.clone(), plan.project_id.clone());
let slot = LedgerSlot {
scope: plan.scope,
project_id: project_id.as_deref(),
file: &file,
container: &pointer,
};
let owned_here = ledger.slot_entries(&slot);
let mut names: Vec<String> = Vec::new();
for member in &plan.input.members {
if !names.contains(&member.name) {
names.push(member.name.clone());
}
}
for row in owned_here.iter() {
if row.kind == EntryKind::ListMember && !names.contains(&row.key) {
names.push(row.key.clone());
}
}
for name in names {
let want = plan.input.members.iter().find(|member| member.name == name);
let owned = owned_here
.get(&name)
.filter(|row| row.kind == EntryKind::ListMember);
let client = match (want, owned) {
(Some(member), _) => Some(member.client),
(None, Some(row)) => adapter(&row.client)
.map(|spec| spec.id)
.filter(|client| plan.input.clients.contains(client)),
(None, None) => None,
};
let Some(client) = client else {
continue;
};
let present = observed.contains(&name);
let member = NativeValue::String(name.clone());
let mut entry = EntryPlan {
name: name.clone(),
client: Some(client),
server_id: want
.map(|member| member.server_id.clone())
.or_else(|| owned.and_then(|row| row.server_id.clone())),
state: EntryState::Synced,
action: Action::None,
unresolved: false,
problem: None,
desired: want.map(|_| member.clone()),
observed: present.then(|| member.clone()),
written: None,
delivery: None,
};
match (want.is_some(), present, owned.is_some()) {
(true, true, false) => {
entry.action = Action::Adopt;
entry.written = Some(member);
}
(true, true, true) => {}
(true, false, false) => {
entry.state = EntryState::PendingCreate;
entry.action = Action::Create;
entry.written = Some(member);
}
(true, false, true) => {
entry.state = EntryState::Missing;
if options.resolution(Some(client), &name) == OnConflict::Skip {
entry.action = Action::Skip;
} else {
entry.action = Action::Create;
entry.written = Some(member);
}
}
(false, true, true)
if entry
.server_id
.as_ref()
.is_some_and(|id| plan.input.invalid_servers.contains(id)) =>
{
entry.state = EntryState::Blocked;
entry.problem = Some(not_loading(entry.server_id.as_deref()));
}
(false, true, true) => {
entry.state = EntryState::PendingRemove;
entry.action = Action::Remove;
}
(false, false, true) => entry.action = Action::Forget,
(false, _, false) => continue,
}
if options.entries.is_some() {
entry.action = Action::None;
entry.written = None;
entry.unresolved = false;
}
plan.entries.push(entry);
}
let mut changed = false;
for entry in &plan.entries {
let result = match entry.action {
Action::Create => doc.set_list_member(&list, &entry.name, true),
Action::Remove => doc.set_list_member(&list, &entry.name, false),
_ => Ok(false),
};
match result {
Ok(edited) => changed |= edited,
Err(err) => {
let err = err.with_path(&plan.file);
return blocked_target(plan, err);
}
}
}
if changed {
let after = doc.to_bytes();
if bytes.as_deref() != Some(after.as_slice()) {
plan.after = Some(after);
}
}
plan
}
pub(crate) fn list_members(doc: &Document, list: &[&str]) -> Result<Vec<String>, CoreError> {
let Some((last, parent)) = list.split_last() else {
return Ok(Vec::new());
};
match doc.get(parent, last)? {
None => Ok(Vec::new()),
Some(NativeValue::Array(items)) => Ok(items
.iter()
.filter_map(|item| item.as_str().map(str::to_owned))
.collect()),
Some(_) => Err(CoreError::new(
ErrorCode::UnexpectedShape,
format!("`{}` is not a list", list.join(".")),
)),
}
}
fn key_spelling(target: &Target, doc: &Document, keys: &[String]) -> Option<CoreError> {
let index = target
.container
.segments()
.iter()
.position(|segment| *segment == ContainerPath::PROJECT_KEY)?;
let ours = keys.get(index)?;
let parent: Vec<&str> = keys[..index].iter().map(String::as_str).collect();
let other = doc
.list(&parent)
.ok()?
.into_iter()
.find(|key| key != ours && same_folder(target.os, key, ours))?;
Some(
CoreError::new(
ErrorCode::ProjectKeySpelling,
format!(
"`{}` holds this folder as {other:?} too; {} writes {ours:?}, and the client \
may read the other key",
parent.join("."),
crate::brand::PRODUCT_NAME
),
)
.with_path(&target.file),
)
}
fn blocked_target(mut plan: TargetPlan, err: CoreError) -> TargetPlan {
let input = &plan.input;
plan.entries = input
.desired
.iter()
.map(|want| {
let mut entry = blocked_entry(
&want.name,
Some(want.client),
Some(&want.server_id),
err.clone(),
);
entry.delivery = want.delivery;
entry
})
.chain(input.members.iter().map(|member| {
blocked_entry(
&member.name,
Some(member.client),
Some(&member.server_id),
err.clone(),
)
}))
.collect();
plan.problem = Some(err);
plan.after = None;
plan
}
fn not_loading(id: Option<&str>) -> CoreError {
CoreError::new(
ErrorCode::ParseError,
format!(
"server {:?} does not load; its entry is left alone",
id.unwrap_or_default()
),
)
}
fn blocked_entry(
name: &str,
client: Option<&'static str>,
server_id: Option<&String>,
err: CoreError,
) -> EntryPlan {
EntryPlan {
name: name.to_owned(),
client,
server_id: server_id.cloned(),
state: EntryState::Blocked,
action: Action::None,
unresolved: false,
problem: Some(err),
desired: None,
observed: None,
written: None,
delivery: None,
}
}
pub fn canonical(client: &str, value: &NativeValue) -> Option<Canonical> {
mapper_of(client).map(|mapper| mapper.canonical(value))
}