Integrations: wire Allow Agent Control and Control Port (#204)

Both fields were in Preferences › Integrations and in
prefs::HIDDEN_UNTIL_IMPLEMENTED: nothing read them, so the only way to open the
control channel was --control (or PHOTOCRAFT_CONTROL_PORT).

They now do what their doc comments describe. Control Port, when it is not 0,
is the port this launch listens on; --control still wins over it. Allow Agent
Control off refuses both, and when --control asked for a port it says so on
stderr instead of dropping the flag silently — an ignored --control with no
message is the shape #2449 was about. A port outside 1..=65535, which only a
hand-edited file can hold, is refused and named the same way.

integrations() reads the saved preferences before the window opens, the way
custom_titlebar() already does for System Title Bar; a missing or unreadable
file keeps the defaults. The defaults are unchanged (allow on, port 0), so a
launch that touches neither preference behaves exactly as before.

Checked against a release build, each case with its own PHOTOCRAFT_CONFIG_DIR:

    preferences                              --control   listens on
    (none)                                   -           nothing
    controlPort 7878                         -           7878
    controlPort 7878                         9000        9000
    allowAgentControl false                  9000        nothing, with the warning
    allowAgentControl false, port 7878       -           nothing
    controlPort 70000                        -           nothing, with the warning

Two tests cover the decision table and the file reading (absent, unparseable,
valid). docs/scorecard.md regenerated: settings that do nothing, 30 -> 28.
This commit is contained in:
Erica Franco committed 2026-10-12 05:11:04 -06:00
1 parent b564f2ca9c
commit 42519bb32d
3 files changed
+84 -5

No files matched your search

+81
View File
@@ -69,6 +69,36 @@ fn custom_titlebar(prefs_file: Option<&std::path::Path>) -> bool {
CUSTOM_TITLEBAR && !prefs.interface.system_title_bar
}
/// Preferences › Integrations, read before the window opens the way [`custom_titlebar`] is:
/// whether agents may drive this launch at all, and the port to listen on when no `--control`
/// was given (0 = only with `--control`). A missing or unreadable file keeps the defaults.
fn integrations(prefs_file: Option<&std::path::Path>) -> photocraft_engine::prefs::Integrations {
let prefs: photocraft_engine::prefs::Preferences =
prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default();
prefs.integrations
}
/// The control port this launch listens on, and the warning to print when a request was refused.
/// `--control` (or `PHOTOCRAFT_CONTROL_PORT`) wins over the preference; Allow Agent Control off
/// refuses both, and says so rather than ignoring the flag silently. A preferred port outside
/// 1..=65535 is refused the same way.
fn control_port_for(cli: Option<u16>, prefs: &photocraft_engine::prefs::Integrations) -> (Option<u16>, Option<String>) {
if !prefs.allow_agent_control {
let warning = cli.map(|p| format!("photocraft: Preferences › Integrations › Allow Agent Control is off: not listening on port {p}\n"));
return (None, warning);
}
if let Some(port) = cli {
return (Some(port), None);
}
match prefs.control_port {
0 => (None, None),
p => match u16::try_from(p).ok().filter(|p| *p != 0) {
Some(port) => (Some(port), None),
None => (None, Some(format!("photocraft: Preferences › Integrations › Control Port {p} is not a usable port: not listening\n"))),
},
}
}
/// The main window: 1440 × 900 (shrunk to fit the monitor, and maximized on the first frame
/// when it still doesn't fit, `work_area::fit_window`), centred on the main monitor. Without
/// `centered`, Windows cascades each new window from the top-left corner, so it opened at a
@@ -256,6 +286,10 @@ fn main() -> eframe::Result {
std::process::exit(1);
}
let (control_port, control_pref_warning) = control_port_for(control_port, &integrations(services::prefs_file().as_deref()));
if let Some(warning) = &control_pref_warning {
eprint!("{warning}");
}
let control = if let Some(port) = control_port {
let (supplied, token_file) = photocraft_automation::security::token_inputs(control_token, control_token_file);
let token = match photocraft_automation::security::server_token(supplied.as_deref(), token_file.as_deref()) {
@@ -645,6 +679,53 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn integrations_preferences_decide_the_control_port() {
use photocraft_engine::prefs::Integrations;
let off = Integrations { allow_agent_control: false, control_port: 0 };
let on = Integrations { allow_agent_control: true, control_port: 0 };
let preferred = Integrations { allow_agent_control: true, control_port: 7878 };
// Nothing asked for, nothing listening.
assert_eq!(super::control_port_for(None, &on), (None, None));
// --control wins over the preference.
assert_eq!(super::control_port_for(Some(9000), &preferred), (Some(9000), None));
// The preference opens the port when --control is absent.
assert_eq!(super::control_port_for(None, &preferred), (Some(7878), None));
// Allow Agent Control off refuses both, and says so when --control asked for one
// rather than ignoring the flag.
assert_eq!(super::control_port_for(None, &off), (None, None));
let (port, warning) = super::control_port_for(Some(9000), &off);
assert_eq!(port, None);
assert!(warning.is_some_and(|w| w.contains("Allow Agent Control") && w.contains("9000")), "the refusal names the port");
let off_with_port = Integrations { allow_agent_control: false, control_port: 7878 };
assert_eq!(super::control_port_for(None, &off_with_port), (None, None), "off beats a preferred port");
// A port a hand-edited file could hold, outside 1..=65535, is refused and named.
let huge = Integrations { allow_agent_control: true, control_port: 70_000 };
let (port, warning) = super::control_port_for(None, &huge);
assert_eq!(port, None);
assert!(warning.is_some_and(|w| w.contains("70000")), "the refusal names the value");
}
#[test]
fn integrations_fall_back_to_the_defaults_without_a_readable_file() {
let dir = std::env::temp_dir().join(format!("pc-integrations-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("prefs.json");
let default = photocraft_engine::prefs::Integrations::default();
assert_eq!(super::integrations(None), default, "no preferences file");
std::fs::write(&file, "not json").unwrap();
assert_eq!(super::integrations(Some(&file)), default, "an unreadable file");
std::fs::write(&file, r#"{"integrations":{"allowAgentControl":false,"controlPort":7878}}"#).unwrap();
let read = super::integrations(Some(&file));
assert!(!read.allow_agent_control);
assert_eq!(read.control_port, 7878);
let _ = std::fs::remove_dir_all(&dir);
}
/// X11 startup notification and docks match a window to its launcher by `StartupWMClass`,
/// so it has to be the class the window carries: the app id, on Wayland and X11 alike.
#[test]
-2
View File
@@ -1058,8 +1058,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"rawDefaults.applyAutoTone",
// Agent access is governed by the launch flags (`--control`, the automation roots), not
// by these yet.
"integrations.allowAgentControl",
"integrations.controlPort",
];
/// Is the preference at `path` (`"section.key"`) hidden from the Preferences dialog?
+3 -3
View File
@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 17 not run, 5 not measurable yet (baseline 2026-10-05) |
| [File compatibility](#file-compatibility) | 6 | 13 | 8 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 |
| [Tools](#tools) | 28 | 14 | 1 | |
| [Workspace and UI](#workspace-and-ui) | 13 | 5 | 5 | settings that do nothing: 30 of 155 (target 0) |
| [Workspace and UI](#workspace-and-ui) | 13 | 5 | 5 | settings that do nothing: 28 of 155 (target 0) |
| [Type](#type) | 4 | 2 | 3 | |
| [Automation](#automation) | 4 | 2 | 5 | 631 of 631 menu items live (100.0%) |
| [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 28 of 28 crates; panic_hunt present |
@@ -176,9 +176,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
## Workspace and UI
**Settings that do nothing: 30 of 155** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
**Settings that do nothing: 28 of 155** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
Unread: `general.color_picker`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `file_handling.save_in_background`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.text_engine`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.
Unread: `general.color_picker`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `file_handling.save_in_background`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.text_engine`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`.
| Id | Target | Status | Issue | Note |
|---|---|---|---|---|