1use std::collections::BTreeMap;
17use std::path::{Path, PathBuf};
18
19use serde::{Deserialize, Serialize};
20
21use crate::error::{Error, Result};
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(deny_unknown_fields)]
25pub struct Config {
26 #[serde(default)]
28 pub activation: ActivationMode,
29 pub remote: RemoteConfig,
30 #[serde(default)]
31 pub local: LocalConfig,
32 #[serde(default)]
33 pub limits: Limits,
34 #[serde(default)]
35 pub audit: AuditConfig,
36 #[serde(default)]
37 pub guard: GuardConfig,
38 #[serde(default)]
39 pub approval: ApprovalConfig,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(deny_unknown_fields)]
44pub struct RemoteConfig {
45 pub host: String,
49 pub workdir: String,
53 #[serde(default)]
57 pub os: Os,
58 #[serde(default = "default_shell")]
64 pub shell: Vec<String>,
65 #[serde(default = "default_connect_timeout")]
66 pub connect_timeout_secs: u64,
67 #[serde(default = "default_alive_interval")]
69 pub server_alive_interval_secs: u64,
70}
71
72pub fn default_shell() -> Vec<String> {
73 vec!["bash".into(), "-lc".into()]
74}
75
76#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "lowercase")]
79pub enum Os {
80 #[default]
81 Auto,
82 Posix,
84 Windows,
86}
87fn default_connect_timeout() -> u64 {
88 20
89}
90fn default_alive_interval() -> u64 {
91 30
92}
93
94#[derive(Debug, Clone, Default, Serialize, Deserialize)]
95#[serde(deny_unknown_fields)]
96pub struct LocalConfig {
97 #[serde(default)]
100 pub allowed_dirs: Vec<PathBuf>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(deny_unknown_fields)]
105pub struct Limits {
106 #[serde(default = "default_command_timeout")]
107 pub command_timeout_secs: u64,
108 #[serde(default = "default_max_command_timeout")]
109 pub max_command_timeout_secs: u64,
110 #[serde(default = "default_max_output_bytes")]
111 pub max_output_bytes: usize,
112 #[serde(default = "default_max_read_bytes")]
113 pub max_read_bytes: usize,
114 #[serde(default = "default_max_transfer_bytes")]
115 pub max_transfer_bytes: u64,
116 #[serde(default = "default_max_transfer_files")]
117 pub max_transfer_files: usize,
118}
119
120fn default_command_timeout() -> u64 {
121 120
122}
123fn default_max_command_timeout() -> u64 {
124 1800
125}
126fn default_max_output_bytes() -> usize {
127 100 * 1024
128}
129fn default_max_read_bytes() -> usize {
130 512 * 1024
131}
132fn default_max_transfer_bytes() -> u64 {
133 200 * 1024 * 1024
134}
135fn default_max_transfer_files() -> usize {
136 5000
137}
138
139impl Default for Limits {
140 fn default() -> Self {
141 Self {
142 command_timeout_secs: default_command_timeout(),
143 max_command_timeout_secs: default_max_command_timeout(),
144 max_output_bytes: default_max_output_bytes(),
145 max_read_bytes: default_max_read_bytes(),
146 max_transfer_bytes: default_max_transfer_bytes(),
147 max_transfer_files: default_max_transfer_files(),
148 }
149 }
150}
151
152#[derive(Debug, Clone, Default, Serialize, Deserialize)]
153#[serde(deny_unknown_fields)]
154pub struct AuditConfig {
155 pub log_dir: Option<PathBuf>,
159}
160
161#[derive(Debug, Clone, Default, Serialize, Deserialize)]
162#[serde(deny_unknown_fields)]
163pub struct GuardConfig {
164 #[serde(default)]
167 pub deny_globs: Vec<String>,
168 #[serde(default)]
170 pub deny_content: Vec<String>,
171}
172
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
176#[serde(rename_all = "lowercase")]
177pub enum ActivationMode {
178 Always,
180 #[default]
185 Project,
186}
187
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub enum Source {
191 Explicit,
192 Env,
193 Project,
194 Global,
195}
196
197#[derive(Debug, Clone)]
200pub struct Loaded {
201 pub config: Config,
202 pub path: PathBuf,
203 pub source: Source,
204}
205
206impl Loaded {
207 pub fn active(&self) -> bool {
208 match self.source {
209 Source::Explicit | Source::Env | Source::Project => true,
210 Source::Global => self.config.activation == ActivationMode::Always,
211 }
212 }
213}
214
215#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
219#[serde(rename_all = "lowercase")]
220pub enum ApprovalMode {
221 Ask,
224 Auto,
226 Strict,
228}
229
230impl ApprovalMode {
231 pub fn as_str(self) -> &'static str {
232 match self {
233 ApprovalMode::Ask => "ask",
234 ApprovalMode::Auto => "auto",
235 ApprovalMode::Strict => "strict",
236 }
237 }
238}
239
240pub const MUTATING_TOOLS: &[&str] = &[
242 "remote_bash",
243 "remote_write",
244 "remote_edit",
245 "upload",
246 "download",
247];
248
249pub const READ_ONLY_TOOLS: &[&str] = &[
251 "remote_read",
252 "remote_ls",
253 "remote_glob",
254 "remote_grep",
255 "local_ls",
256 "local_read",
257 "remote_info",
258];
259
260#[derive(Debug, Clone, Serialize, Deserialize)]
261#[serde(deny_unknown_fields)]
262pub struct ApprovalConfig {
263 #[serde(default = "default_approval")]
265 pub mode: ApprovalMode,
266 #[serde(default)]
268 pub tools: BTreeMap<String, ApprovalMode>,
269}
270
271fn default_approval() -> ApprovalMode {
272 ApprovalMode::Ask
273}
274
275impl Default for ApprovalConfig {
276 fn default() -> Self {
277 Self {
278 mode: default_approval(),
279 tools: BTreeMap::new(),
280 }
281 }
282}
283
284impl ApprovalConfig {
285 pub fn effective(&self) -> BTreeMap<&'static str, ApprovalMode> {
288 let mutating = match self.mode {
289 ApprovalMode::Auto => ApprovalMode::Auto,
290 ApprovalMode::Ask | ApprovalMode::Strict => ApprovalMode::Ask,
291 };
292 let read_only = match self.mode {
293 ApprovalMode::Strict => ApprovalMode::Ask,
294 ApprovalMode::Ask | ApprovalMode::Auto => ApprovalMode::Auto,
295 };
296 MUTATING_TOOLS
297 .iter()
298 .map(|t| (*t, mutating))
299 .chain(READ_ONLY_TOOLS.iter().map(|t| (*t, read_only)))
300 .map(|(t, m)| (t, self.tools.get(t).copied().unwrap_or(m)))
301 .collect()
302 }
303}
304
305impl Config {
306 pub fn example() -> &'static str {
307 EXAMPLE
308 }
309
310 pub fn load(explicit: Option<&Path>) -> Result<(Self, PathBuf)> {
313 let l = Self::load_in(None, explicit)?;
314 Ok((l.config, l.path))
315 }
316
317 pub fn load_in(cwd: Option<&Path>, explicit: Option<&Path>) -> Result<Loaded> {
320 let (path, source) = match explicit {
321 Some(p) => (p.to_path_buf(), Source::Explicit),
322 None => Self::locate(cwd)?,
323 };
324 let text = std::fs::read_to_string(&path)
325 .map_err(|e| Error::Config(format!("cannot read {}: {e}", path.display())))?;
326 let base = match source {
330 Source::Global => None,
331 _ => path.parent().map(Path::to_path_buf),
332 };
333 let config = Self::parse_at(&text, base.as_deref())?;
334 Ok(Loaded {
335 config,
336 path,
337 source,
338 })
339 }
340
341 pub fn parse(text: &str) -> Result<Self> {
344 Self::parse_at(text, None)
345 }
346
347 pub fn parse_at(text: &str, base: Option<&Path>) -> Result<Self> {
350 let mut cfg: Config = toml::from_str(text).map_err(|e| Error::Config(e.to_string()))?;
351 cfg.validate(base)?;
352 Ok(cfg)
353 }
354
355 fn locate(cwd: Option<&Path>) -> Result<(PathBuf, Source)> {
356 if let Some(p) = std::env::var_os("FARHAND_CONFIG") {
357 return Ok((PathBuf::from(p), Source::Env));
358 }
359 let local = match cwd {
360 Some(d) => d.join(".farhand.toml"),
361 None => PathBuf::from(".farhand.toml"),
362 };
363 if local.is_file() {
364 return Ok((local, Source::Project));
365 }
366 let p = Self::global_path();
367 if p.is_file() {
368 return Ok((p, Source::Global));
369 }
370 Err(Error::NoConfig)
371 }
372
373 pub fn global_path() -> PathBuf {
374 xdg_dir("XDG_CONFIG_HOME", ".config")
375 .join("farhand")
376 .join("config.toml")
377 }
378
379 fn validate(&mut self, base: Option<&Path>) -> Result<()> {
380 if self.remote.host.trim().is_empty() {
381 return Err(Error::Config("remote.host is empty".into()));
382 }
383 if self.remote.host.starts_with('-') {
384 return Err(Error::Config("remote.host may not start with '-'".into()));
385 }
386 let raw = self.remote.workdir.trim();
387 let w = raw.trim_end_matches(['/', '\\']);
388 let w = if w.is_empty() && raw.starts_with('/') {
389 "/".to_string()
390 } else if is_drive_root(raw) {
391 format!("{}\\", &raw[..2])
393 } else {
394 w.to_string()
395 };
396 let windows_abs = w.len() >= 3
397 && w.as_bytes()[0].is_ascii_alphabetic()
398 && w.as_bytes()[1] == b':'
399 && (w.as_bytes()[2] == b'\\' || w.as_bytes()[2] == b'/');
400 if !(w.starts_with('/')
401 || w == "~"
402 || w.starts_with("~/")
403 || w.starts_with("~\\")
404 || windows_abs)
405 {
406 return Err(Error::Config(
407 "remote.workdir must be an absolute path (`/srv/app`, `C:\\work`) or start with `~` \
408 (the remote home)"
409 .into(),
410 ));
411 }
412 self.remote.workdir = w;
413 if self.remote.shell.is_empty() {
414 return Err(Error::Config("remote.shell is empty".into()));
415 }
416 if self.limits.command_timeout_secs == 0 || self.limits.max_command_timeout_secs == 0 {
417 return Err(Error::Config("timeouts must be positive".into()));
418 }
419 let mut dirs = Vec::with_capacity(self.local.allowed_dirs.len());
420 let home = dirs::home_dir();
421 for d in &self.local.allowed_dirs {
422 let mut expanded = expand_home(d);
423 if !expanded.is_absolute() {
424 let Some(base) = base else {
428 return Err(Error::Config(format!(
429 "local.allowed_dirs entry `{}` is relative; only a project \
430 .farhand.toml may use relative entries (`\".\"` for its own folder)",
431 d.display()
432 )));
433 };
434 if expanded
435 .components()
436 .any(|c| matches!(c, std::path::Component::ParentDir))
437 {
438 return Err(Error::Config(format!(
439 "local.allowed_dirs entry `{}` may not contain `..`",
440 d.display()
441 )));
442 }
443 expanded = base.join(expanded);
444 }
445 if expanded.parent().is_none() || home.as_deref() == Some(expanded.as_path()) {
450 return Err(Error::Config(format!(
451 "local.allowed_dirs may not contain `/` or the home directory ({}); \
452 allow a specific folder instead",
453 d.display()
454 )));
455 }
456 dirs.push(expanded);
457 }
458 self.local.allowed_dirs = dirs;
459 if let Some(d) = &self.audit.log_dir {
460 self.audit.log_dir = Some(expand_home(d));
461 }
462 for (t, m) in &self.approval.tools {
463 if !MUTATING_TOOLS.contains(&t.as_str()) && !READ_ONLY_TOOLS.contains(&t.as_str()) {
464 return Err(Error::Config(format!(
465 "approval.tools: `{t}` is not a tool (one of {}, {})",
466 MUTATING_TOOLS.join(", "),
467 READ_ONLY_TOOLS.join(", ")
468 )));
469 }
470 if *m == ApprovalMode::Strict {
471 return Err(Error::Config(format!(
472 "approval.tools.{t}: use `ask` or `auto` (`strict` is a global mode)"
473 )));
474 }
475 }
476 Ok(())
477 }
478
479 pub fn audit_dir(&self) -> PathBuf {
480 self.audit.log_dir.clone().unwrap_or_else(|| {
481 xdg_dir("XDG_DATA_HOME", ".local/share")
482 .join("farhand")
483 .join("audit")
484 })
485 }
486}
487
488fn xdg_dir(var: &str, fallback: &str) -> PathBuf {
492 if let Some(v) = std::env::var_os(var) {
493 if !v.is_empty() {
494 return PathBuf::from(v);
495 }
496 }
497 dirs::home_dir()
498 .unwrap_or_else(|| PathBuf::from("."))
499 .join(fallback)
500}
501
502fn is_drive_root(s: &str) -> bool {
505 let b = s.as_bytes();
506 (b.len() == 2 || b.len() == 3)
507 && b[0].is_ascii_alphabetic()
508 && b[1] == b':'
509 && (b.len() == 2 || b[2] == b'\\' || b[2] == b'/')
510}
511
512pub fn expand_home(p: &Path) -> PathBuf {
513 let s = p.to_string_lossy();
514 if s == "~" {
515 return dirs::home_dir().unwrap_or_else(|| p.to_path_buf());
516 }
517 if let Some(rest) = s.strip_prefix("~/") {
518 if let Some(home) = dirs::home_dir() {
519 return home.join(rest);
520 }
521 }
522 p.to_path_buf()
523}
524
525const EXAMPLE: &str = r#"# FarHand configuration.
526# As .farhand.toml in a project root, it makes that project remote.
527# As ~/.config/farhand/config.toml it is optional: only needed for
528# activation = "always" (or as the target of --config / FARHAND_CONFIG).
529
530# "project" (default): only directories that carry a .farhand.toml are
531# remote; anywhere else the agent keeps its local tools and FarHand
532# stays out of the way.
533# "always": every agent session on this machine is remote, using the
534# [remote] section below.
535# Only meaningful in this global file; a project's .farhand.toml is always
536# active in its own directory.
537# activation = "project"
538
539[remote]
540# An alias from ~/.ssh/config or user@host. Keys, ports, jump hosts and
541# agent forwarding all come from your OpenSSH config.
542host = "devbox"
543# Directory on the remote where every command starts and relative paths
544# resolve. Absolute, or `~/...` for the remote user's home.
545workdir = "~/project"
546# os = "auto" # "posix" or "windows"; detected on connect, but set it so
547# # the model knows which shell it is writing for
548# shell = ["bash", "-lc"] # Windows: ["powershell"] (default) or ["pwsh"]
549# connect_timeout_secs = 20
550# server_alive_interval_secs = 30
551
552[local]
553# The only local directories the model may list, read from, upload from or
554# download into. Leave empty to close the local filesystem completely.
555# In a project .farhand.toml, "." means this folder: drop files here to
556# upload them, and downloads land here. Never allow your home directory.
557allowed_dirs = ["."]
558
559[limits]
560# command_timeout_secs = 120
561# max_command_timeout_secs = 1800
562# max_output_bytes = 102400
563# max_read_bytes = 524288
564# max_transfer_bytes = 209715200
565# max_transfer_files = 5000
566
567[audit]
568# log_dir = "~/.local/share/farhand/audit"
569
570[guard]
571# Extra globs that may never leave this machine (added to the built-in list).
572# deny_globs = ["*.internal"]
573# Extra regexes that mark content as secret.
574# deny_content = ["ACME-[0-9]{12}"]
575
576[approval]
577# "ask": the agent prompts you before remote_bash, remote_write,
578# remote_edit, upload and download; read-only tools run freely.
579# "auto": nothing prompts; the audit log is your record.
580# "strict": every tool prompts, reads and searches included.
581mode = "ask"
582# Per-tool overrides (`ask` or `auto`), any tool name:
583# tools = { remote_write = "auto", remote_edit = "auto", remote_read = "ask" }
584"#;
585
586#[cfg(test)]
587mod tests {
588 use super::*;
589
590 #[test]
591 fn example_parses() {
592 let cfg = Config::parse_at(Config::example(), Some(Path::new("/proj"))).unwrap();
595 assert_eq!(cfg.remote.host, "devbox");
596 assert_eq!(cfg.remote.shell, vec!["bash", "-lc"]);
597 assert_eq!(cfg.local.allowed_dirs, vec![PathBuf::from("/proj")]);
598 assert!(Config::parse(Config::example()).is_err());
599 }
600
601 #[test]
602 fn rejects_home_and_root_as_allowed_dirs() {
603 for d in ["/", "~"] {
604 let err = Config::parse(&format!(
605 "[remote]\nhost='h'\nworkdir='/w'\n[local]\nallowed_dirs=['{d}']"
606 ))
607 .unwrap_err();
608 assert!(err.to_string().contains("home directory"), "{d}: {err}");
609 }
610 assert!(
611 Config::parse("[remote]\nhost='h'\nworkdir='/w'\n[local]\nallowed_dirs=['~/out']")
612 .is_ok()
613 );
614 }
615
616 #[test]
617 fn relative_allowed_dirs_resolve_against_the_project_file() {
618 let text = "[remote]\nhost='h'\nworkdir='/w'\n[local]\nallowed_dirs=['.', 'assets']";
619 let err = Config::parse(text).unwrap_err();
620 assert!(err.to_string().contains("relative"), "{err}");
621 let cfg = Config::parse_at(text, Some(Path::new("/proj"))).unwrap();
622 assert_eq!(
623 cfg.local.allowed_dirs,
624 vec![PathBuf::from("/proj"), PathBuf::from("/proj/assets")]
625 );
626 let err = Config::parse_at(
627 "[remote]\nhost='h'\nworkdir='/w'\n[local]\nallowed_dirs=['../x']",
628 Some(Path::new("/proj")),
629 )
630 .unwrap_err();
631 assert!(err.to_string().contains(".."), "{err}");
632 }
633
634 #[test]
635 fn rejects_relative_workdir() {
636 let err = Config::parse("[remote]\nhost='h'\nworkdir='rel'").unwrap_err();
637 assert!(err.to_string().contains("absolute"));
638 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='~/'").unwrap();
639 assert_eq!(cfg.remote.workdir, "~");
640 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='/srv/app/'").unwrap();
641 assert_eq!(cfg.remote.workdir, "/srv/app");
642 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='C:\\work\\'").unwrap();
643 assert_eq!(cfg.remote.workdir, "C:\\work");
644 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='D:/'").unwrap();
645 assert_eq!(cfg.remote.workdir, "D:\\");
646 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='C:'").unwrap();
647 assert_eq!(cfg.remote.workdir, "C:\\");
648 assert!(Config::parse("[remote]\nhost='h'\nworkdir='work'").is_err());
649 let cfg = Config::parse("[remote]\nhost='h'\nworkdir='/'\nos='windows'").unwrap();
650 assert_eq!(cfg.remote.os, Os::Windows);
651 }
652
653 #[test]
654 fn approval_overrides_apply() {
655 let cfg = Config::parse(
656 "[remote]\nhost='h'\nworkdir='/'\n[approval]\nmode='auto'\ntools={ remote_bash='ask' }",
657 )
658 .unwrap();
659 let e = cfg.approval.effective();
660 assert_eq!(e["remote_bash"], ApprovalMode::Ask);
661 assert_eq!(e["remote_write"], ApprovalMode::Auto);
662 assert_eq!(e["remote_read"], ApprovalMode::Auto);
663 assert_eq!(e.len(), MUTATING_TOOLS.len() + READ_ONLY_TOOLS.len());
664
665 let strict = Config::parse(
666 "[remote]\nhost='h'\nworkdir='/'\n[approval]\nmode='strict'\ntools={ remote_info='auto' }",
667 )
668 .unwrap();
669 let e = strict.approval.effective();
670 assert_eq!(e["remote_read"], ApprovalMode::Ask);
671 assert_eq!(e["remote_bash"], ApprovalMode::Ask);
672 assert_eq!(e["remote_info"], ApprovalMode::Auto);
673
674 let default = Config::parse("[remote]\nhost='h'\nworkdir='/'").unwrap();
675 assert_eq!(
676 default.approval.effective()["remote_read"],
677 ApprovalMode::Auto
678 );
679 assert!(Config::parse(
680 "[remote]\nhost='h'\nworkdir='/'\n[approval]\ntools={ nope='auto' }"
681 )
682 .is_err());
683 assert!(Config::parse(
684 "[remote]\nhost='h'\nworkdir='/'\n[approval]\ntools={ upload='strict' }"
685 )
686 .is_err());
687 }
688
689 #[test]
690 fn activation_depends_on_source() {
691 let mk = |activation: &str, source: Source| Loaded {
692 config: Config::parse(&format!(
693 "activation='{activation}'\n[remote]\nhost='h'\nworkdir='/'"
694 ))
695 .unwrap(),
696 path: PathBuf::new(),
697 source,
698 };
699 assert!(mk("always", Source::Global).active());
700 assert!(!mk("project", Source::Global).active());
701 assert!(mk("project", Source::Project).active());
702 assert!(mk("project", Source::Explicit).active());
703 assert_eq!(
704 Config::parse("[remote]\nhost='h'\nworkdir='/'")
705 .unwrap()
706 .activation,
707 ActivationMode::Project
708 );
709 }
710
711 #[test]
712 fn rejects_unknown_fields() {
713 assert!(Config::parse("[remote]\nhost='h'\nworkdir='/'\nbogus=1").is_err());
714 }
715}