Keep nightly default agent files inside their selected profile

Reuse the SDK resolved stable Documents boundary and existing workspace injection. Reject profile overlap and escaping default-folder aliases without stable fallback. Five added regressions are authored; execution is pending.
This commit is contained in:
skylartaylor committed 2026-11-21 09:03:52 +01:00
1 parent 7bf69f0dec
commit 0998d5e2a1
3 files changed
+172 -6

No files matched your search

+2 -1
View File
@@ -101,7 +101,8 @@ pub fn prepare(mut launch: Launch, validate: impl FnOnce(&Path) -> Result<PathBu
/// Resolve the selected private root before the caller's first startup effect.
pub fn validate_root(root: &Path, env: impl Fn(&str) -> Option<OsString>, exe: Option<&Path>) -> Result<PathBuf, String> {
crate::profile_root::validate(root, env, exe, photocraft_automation::app_dirs::ensure_writable)
let stable_agent = craft::control::agents::default_folder(crate::AGENT_APP_NAME);
crate::profile_root::validate(root, env, exe, stable_agent.as_deref(), photocraft_automation::app_dirs::ensure_writable)
}
#[cfg(test)]
+63 -3
View File
@@ -87,8 +87,9 @@ fn title_bar_pref(prefs: &photocraft_engine::prefs::Preferences) -> TitleBarPref
/// The automation roots: `--automation-read-root` / `--automation-write-root` (or their
/// variables) win; a direction without one takes Preferences › AI Integrations' folder, and with
/// that empty too the default agent folder `Documents/ArtCraft/Agents/PhotoCraft`, created when
/// first used (`AgentSettings::folders`). A flag's
/// that empty too the channel's default agent folder: stable uses
/// `Documents/ArtCraft/Agents/PhotoCraft`; Nightly uses its private profile's `AgentFiles`.
/// It is created only when first used (`AgentSettings::folders_with`). A flag's
/// root that can't be opened stops the launch (exit status 2) as before. A preference's root that
/// can't be opened, or isn't an absolute path (it would depend on the folder the app was started
/// from), is left out, so that direction fails closed, and the warning says why: a folder moved
@@ -99,7 +100,20 @@ fn automation_workspace(
flags: &photocraft_automation::args::AutomationArgs,
prefs: &craft::automation::agents::AgentSettings,
) -> Result<(photocraft_automation::workspace::AuthorizedWorkspace, Option<String>), photocraft_automation::AutomationError> {
automation_workspace_in(flags, prefs, craft::control::agents::default_folder(AGENT_APP_NAME).as_deref())
automation_workspace_for_profile(flags, prefs, launch_profile::current(), || craft::control::agents::default_folder(AGENT_APP_NAME))
}
fn automation_workspace_for_profile(
flags: &photocraft_automation::args::AutomationArgs,
prefs: &craft::automation::agents::AgentSettings,
profile: &launch_profile::Profile,
stable_default: impl FnOnce() -> Option<std::path::PathBuf>,
) -> Result<(photocraft_automation::workspace::AuthorizedWorkspace, Option<String>), photocraft_automation::AutomationError> {
let default = match profile.root() {
Some(root) => Some(profile_root::agent_directory(root).map_err(photocraft_automation::AutomationError::BadRequest)?),
None => stable_default(),
};
automation_workspace_in(flags, prefs, default.as_deref())
}
/// The app's name in its default agent folder, `Documents/ArtCraft/Agents/PhotoCraft`.
@@ -1165,6 +1179,52 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn nightly_agent_workspace_uses_its_private_profile_not_stable_documents() {
let dir = temp_dir("nightly-agent-roots");
let private = dir.join("nightly profile");
let stable = dir.join("redirected Documents/ArtCraft/Agents/PhotoCraft");
std::fs::create_dir_all(&private).unwrap();
std::fs::create_dir_all(&stable).unwrap();
std::fs::write(stable.join("existing-document"), b"stable document bytes").unwrap();
let profile = super::launch_profile::Profile::NightlyV1(private.clone());
let (ws, warning) = super::automation_workspace_for_profile(&flags(None, None), &craft::automation::agents::AgentSettings::default(), &profile, || {
panic!("Nightly must never consult the stable agent-folder fallback")
})
.unwrap();
assert_eq!(warning, None);
let canonical_private = std::fs::canonicalize(&private).unwrap();
assert!(ws.read_folders().iter().all(|p| p.starts_with(&canonical_private)));
assert!(ws.write_folder().unwrap().starts_with(&canonical_private));
ws.write("nightly-document", b"nightly document bytes").unwrap();
assert_eq!(ws.read("nightly-document").unwrap(), b"nightly document bytes");
assert_eq!(std::fs::read(stable.join("existing-document")).unwrap(), b"stable document bytes");
assert_eq!(std::fs::read_dir(&stable).unwrap().count(), 1);
drop(ws);
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn stable_agent_workspace_preserves_the_resolved_default_folder() {
let dir = temp_dir("stable-agent-roots");
let stable = dir.join("redirected Documents/ArtCraft/Agents/PhotoCraft");
let (ws, warning) = super::automation_workspace_for_profile(
&flags(None, None),
&craft::automation::agents::AgentSettings::default(),
&super::launch_profile::Profile::Stable,
|| Some(stable.clone()),
)
.unwrap();
assert_eq!(warning, None);
let canonical_stable = std::fs::canonicalize(&stable).unwrap();
assert_eq!(ws.read_folders(), vec![canonical_stable.clone()]);
assert_eq!(ws.write_folder(), Some(canonical_stable));
ws.write("stable-document", b"stable document bytes").unwrap();
assert_eq!(std::fs::read(stable.join("stable-document")).unwrap(), b"stable document bytes");
drop(ws);
std::fs::remove_dir_all(dir).unwrap();
}
/// Empty preferences fall back to the default agent folder, created when first used, for
/// both directions (one shared capability); a flag or the person's own folder still wins.
#[test]
+107 -2
View File
@@ -123,15 +123,28 @@ fn forbidden_roots(platform: Platform, env: &impl Fn(&str) -> Option<OsString>,
roots.into_iter().map(|p| canonical_future(&p)).collect()
}
/// The nightly fallback for files made by agents. Resolving it never creates a directory and
/// never falls back to the stable Documents folder. The SDK creates it lazily if a default is
/// actually needed; explicit user-chosen document roots keep their existing semantics.
pub fn agent_directory(root: &Path) -> Result<PathBuf, String> {
let chosen = canonical_future(&absolute(root.to_path_buf(), "the nightly profile root")?)?;
let agent = canonical_future(&chosen.join("AgentFiles"))?;
if agent == chosen || !agent.starts_with(&chosen) {
return Err("the nightly default agent folder must stay inside its selected profile".into());
}
Ok(agent)
}
/// The writable hook is the app's existing exclusive probe. Keeping it injected lets filesystem
/// boundary tests exercise the real policy without starting the app or changing global state.
pub fn validate(
root: &Path,
env: impl Fn(&str) -> Option<OsString>,
exe: Option<&Path>,
stable_agent: Option<&Path>,
writable: impl FnOnce(&Path) -> std::io::Result<()>,
) -> Result<PathBuf, String> {
validate_for(Platform::current()?, root, env, exe, writable)
validate_for(Platform::current()?, root, env, exe, stable_agent, writable)
}
fn validate_for(
@@ -139,6 +152,7 @@ fn validate_for(
root: &Path,
env: impl Fn(&str) -> Option<OsString>,
exe: Option<&Path>,
stable_agent: Option<&Path>,
writable: impl FnOnce(&Path) -> std::io::Result<()>,
) -> Result<PathBuf, String> {
absolute(root.to_path_buf(), "the nightly profile root")?;
@@ -152,10 +166,17 @@ fn validate_for(
}
let chosen = canonical_future(root)?;
let exe = absolute(exe.ok_or("cannot establish the installation boundary")?.to_path_buf(), "the executable")?;
let forbidden = forbidden_roots(platform, &env, &exe)?;
let mut forbidden = forbidden_roots(platform, &env, &exe)?;
if let Some(agent) = stable_agent {
// Use the SDK-resolved Documents/known-folder location, including redirected Documents.
// Do not reconstruct it from HOME or create it just to establish the boundary.
forbidden.push(canonical_future(&absolute(agent.to_path_buf(), "the stable default agent folder")?)?);
}
if forbidden.iter().any(|stable| overlaps(&chosen, stable, platform)) {
return Err("the nightly root must not overlap stable data or an installation".into());
}
// Existing aliases under the profile must not redirect the default agent workspace elsewhere.
agent_directory(&chosen)?;
let mut create = std::fs::DirBuilder::new();
create.recursive(true);
#[cfg(unix)]
@@ -168,6 +189,7 @@ fn validate_for(
if confirmed != chosen || forbidden.iter().any(|stable| overlaps(&confirmed, stable, platform)) {
return Err("the nightly profile changed while it was being validated".into());
}
agent_directory(&confirmed)?;
writable(&confirmed).map_err(|e| format!("the nightly profile is not writable: {e}"))?;
Ok(confirmed)
}
@@ -217,6 +239,7 @@ mod tests {
root,
|key| self.get(key),
Some(&self.exe),
None,
|dir| {
let probe = dir.join("exclusive-test-probe");
let file = std::fs::OpenOptions::new().write(true).create_new(true).open(&probe)?;
@@ -238,6 +261,87 @@ mod tests {
}
}
#[test]
fn agent_default_is_private_lazy_and_preserves_stable_documents() {
let fixture = Fixture::new();
let stable = fixture.home().join("Documents/ArtCraft/Agents/PhotoCraft");
std::fs::create_dir_all(&stable).unwrap();
std::fs::write(stable.join("existing-document"), b"stable document bytes").unwrap();
let private = fixture.check(Platform::Linux, &fixture.home().join("nightly-profile")).unwrap();
let agent = agent_directory(&private).unwrap();
assert!(agent.starts_with(&private) && agent != private);
assert!(!agent.exists(), "resolving the fallback must not create it before use");
std::fs::create_dir_all(&agent).unwrap();
std::fs::write(agent.join("new-document"), b"nightly document bytes").unwrap();
assert_eq!(std::fs::read(stable.join("existing-document")).unwrap(), b"stable document bytes");
assert_eq!(std::fs::read_dir(&stable).unwrap().count(), 1);
assert_eq!(std::fs::read(agent.join("new-document")).unwrap(), b"nightly document bytes");
}
#[test]
fn stable_agent_known_folder_and_overlaps_refuse_before_writable_probe() {
let fixture = Fixture::new();
// Models redirected Documents/known folders: not assumed to be beneath HOME.
let stable = fixture.root.join("redirected documents/ArtCraft/Agents/PhotoCraft");
std::fs::create_dir_all(&stable).unwrap();
std::fs::write(stable.join("existing-document"), b"stable document bytes").unwrap();
for platform in [Platform::Windows, Platform::Linux, Platform::Mac] {
for root in [stable.clone(), stable.join("nightly"), stable.parent().unwrap().to_path_buf()] {
let calls = std::cell::Cell::new(0);
let result = validate_for(
platform,
&root,
|key| fixture.get(key),
Some(&fixture.exe),
Some(&stable),
|_| {
calls.set(calls.get() + 1);
Ok(())
},
);
assert!(result.unwrap_err().contains("overlap"));
assert_eq!(calls.get(), 0);
}
}
assert_eq!(std::fs::read(stable.join("existing-document")).unwrap(), b"stable document bytes");
assert_eq!(std::fs::read_dir(&stable).unwrap().count(), 1);
}
#[test]
#[cfg(unix)]
fn agent_default_rejects_existing_and_dangling_escape_aliases_before_writes() {
let fixture = Fixture::new();
let private = fixture.home().join("nightly-profile");
let outside = fixture.home().join("stable-agent-files");
std::fs::create_dir_all(&private).unwrap();
std::fs::create_dir_all(&outside).unwrap();
std::fs::write(outside.join("existing-document"), b"stable document bytes").unwrap();
for target in [&outside, &outside.join("missing")] {
let alias = private.join("AgentFiles");
std::os::unix::fs::symlink(target, &alias).unwrap();
assert!(agent_directory(&private).is_err());
let calls = std::cell::Cell::new(0);
assert!(
validate_for(
Platform::Linux,
&private,
|key| fixture.get(key),
Some(&fixture.exe),
Some(&outside),
|_| {
calls.set(calls.get() + 1);
Ok(())
}
)
.is_err()
);
assert_eq!(calls.get(), 0);
assert_eq!(std::fs::read_dir(&outside).unwrap().count(), 1);
std::fs::remove_file(alias).unwrap();
}
assert_eq!(std::fs::read(outside.join("existing-document")).unwrap(), b"stable document bytes");
}
#[test]
fn overlap_check_handles_ancestor_descendant_and_prefix_lookalikes() {
let fixture = Fixture::new();
@@ -339,6 +443,7 @@ mod tests {
&candidate,
|key| fixture.get(key),
Some(&fixture.exe),
None,
|_| {
calls.set(calls.get() + 1);
Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "injected write refusal"))