From ffd84b829eaa59f5bfcd97432d321a782bd93d6f Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sat, 14 Nov 2026 14:41:14 +0000 Subject: [PATCH 1/3] fix: address PR #30 review findings Keep expired approvals from filling pending slots; replay legacy reserved profile variables while letting the runner value win; require a Windows cache root, enforce the overall fetch deadline, and publish without replacing; withdraw runs on cache-root or model_tools changes and revoke covering cache grants; fix UTF-8 summary truncation; make disk sampling cancellable with bounded queues and time; correct the model-tool and approval contracts; add regression tests for each fix. Co-Authored-By: Claude Code --- AGENTS.md | 8 +- CHANGELOG.md | 34 +++- crates/omgcode-cli/src/control_cli.rs | 10 +- crates/omgcode-cli/src/run_cli.rs | 1 + crates/omgcode-cli/tests/packages_workflow.rs | 22 +++ crates/omgcode-cli/tests/run_workflow.rs | 18 +- crates/omgcode-core/src/control.rs | 110 +++++++++++- crates/omgcode-core/src/control/approval.rs | 11 +- .../src/control/approval/tests.rs | 27 +++ crates/omgcode-core/src/control/profile.rs | 17 +- .../omgcode-core/src/control/profile/tests.rs | 47 ++++- crates/omgcode-core/src/packages.rs | 61 +++++-- crates/omgcode-core/src/packages/tests.rs | 170 +++++++++++++++++- crates/omgcode-core/src/run.rs | 92 ++++++++++ crates/omgcode-core/src/run_journal.rs | 19 +- crates/omgcode-core/src/run_journal/tests.rs | 22 ++- crates/omgcode-core/src/turn.rs | 22 ++- crates/omgcode-core/tests/control_store.rs | 33 ++-- crates/omgcode-platform/src/process/disk.rs | 98 ++++++++-- docs/README.md | 16 +- ...l-with-approvals-and-the-trusted-runner.md | 15 +- docs/execution.md | 25 ++- docs/security-coding-rules.md | 7 +- docs/testing-standards.md | 6 +- 24 files changed, 795 insertions(+), 96 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 2bcc78c..b21f743 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -25,9 +25,11 @@ filesystem quota. Package-network profiles are recorded as configuration only; no run uses them. The trusted `packages fetch` command downloads from declared origins into a per-workspace cache. A selected root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant, and Linux -falls back to application storage. Provider profiles and trusted single-use -approval records are configuration and authorization records only; no adapter -or model tool uses them. Trusted CLI +falls back to application storage. Provider profiles record connection shapes +without credentials or adapters; no adapter or login uses them. Trusted +single-use approval records authorize one exact model-tool call: only a later +identical call may consume an approved record and invoke the trusted runner, +and `workspace_run_command` is their only consumer. Trusted CLI commands record, apply, and revoke Windows container ACL grants. Follow the [execution contract](docs/execution.md). The bundled web interface and most planned features are not implemented. diff --git a/CHANGELOG.md b/CHANGELOG.md index 19408ad..aaac690 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,9 @@ Changes are recorded here before a release version is assigned. preferences are enabled. Every call rechecks them; the trusted run checks them again. - **Arguments:** a recorded toolchain profile, program, at most 64 - arguments, and an optional workspace-relative `cwd`. There is no shell. + arguments, and an optional workspace-relative `cwd`. The tool has no shell + mode and never re-parses a command line; the model may name an interpreter + the approved profile exposes. - **Approval:** the first call records a pending approval and returns `approval_required`; it starts nothing. After `approvals approve `, a second identical call consumes the approval and calls the trusted runner. @@ -336,6 +338,36 @@ Changes are recorded here before a release version is assigned. ### Fixed +- PR #30 review follow-up: + - Expired, undecided approvals no longer occupy the 64 pending slots during + requests or replay. Only records unexpired at the request's event time + count. + - A legacy toolchain profile that declares `OMGCODE_PACKAGE_CACHE` still + replays. New declarations are refused; the runner skips a legacy value so + its own cache path wins. + - Windows package fetch now requires a selected cache root, matching the + runner's lack of an application-storage fallback. + - Package fetch checks its overall deadline before each body read, so a + trickled response cannot outlive the requested timeout. + - Package cache publication uses an atomic no-replace hard link. Two + concurrent fetches cannot overwrite each other, and a synchronized + collision test covers the winner. + - A run records its configured package cache root and whether it came from a + model tool. Clearing or replacing the root, or disabling `model_tools`, + withdraws the live run. The cache-root CLI commands wait for affected runs. + - Changing or clearing a cache root revokes read-execute container grants + that covered it, and selecting a root another workspace can already read is + refused. + - The model command summary finds a UTF-8 boundary before truncation; a long + multibyte program name no longer panics. + - Disk sampling observes cancellation during traversal, and directory + enumeration checks the entry budget before queuing each path. + - Documents that `workspace_run_command` has no shell mode but may name an + interpreter under the exact-request approval. Corrects stale plan, guide, + and AGENTS descriptions of approval records and model tools. + - Strengthens the ungranted-cache refusal test to prove the program did not + start, and the cache-root replay test to compare the exact workspace and + directory identity. - Tree limits, after the PR #27 review: - **Range checks before recording:** the core checks every run limit before it records a run, so a direct caller with an out-of-range tree limit gets diff --git a/crates/omgcode-cli/src/control_cli.rs b/crates/omgcode-cli/src/control_cli.rs index 7cbc394..56c45e3 100644 --- a/crates/omgcode-cli/src/control_cli.rs +++ b/crates/omgcode-cli/src/control_cli.rs @@ -255,7 +255,10 @@ pub(crate) fn run(action: WorkspaceCommand, data_root: &Path) -> Result<(), CliE store .set_package_cache_root(workspace_id, &path, revision) .map_err(control_error)?; - print_json(&store.snapshot()) + let snapshot = store.snapshot(); + drop(store); + stop_withdrawn_runs(data_root, &[])?; + print_json(&snapshot) } WorkspaceCommand::ClearPackageCache { workspace, @@ -267,7 +270,10 @@ pub(crate) fn run(action: WorkspaceCommand, data_root: &Path) -> Result<(), CliE store .clear_package_cache_root(workspace_id, revision) .map_err(control_error)?; - print_json(&store.snapshot()) + let snapshot = store.snapshot(); + drop(store); + stop_withdrawn_runs(data_root, &[])?; + print_json(&snapshot) } WorkspaceCommand::Register { path, revision } => { let mut store = open(data_root)?; diff --git a/crates/omgcode-cli/src/run_cli.rs b/crates/omgcode-cli/src/run_cli.rs index 8921f31..0be145c 100644 --- a/crates/omgcode-cli/src/run_cli.rs +++ b/crates/omgcode-cli/src/run_cli.rs @@ -126,6 +126,7 @@ pub(crate) fn run(args: RunArgs, data_root: &Path) -> Result<(), CliError> { working_directory: args.cwd, host_services: args.host_services, network: args.network, + model_tools: false, limits: ProcessLimits { deadline, max_stdout_bytes: args.max_output_bytes, diff --git a/crates/omgcode-cli/tests/packages_workflow.rs b/crates/omgcode-cli/tests/packages_workflow.rs index 03b1c27..e51f924 100644 --- a/crates/omgcode-cli/tests/packages_workflow.rs +++ b/crates/omgcode-cli/tests/packages_workflow.rs @@ -163,6 +163,25 @@ fn a_trusted_fetch_writes_a_private_cache_and_is_audited() { ); assert_success(&output); + // Windows has no application-storage fallback; select an external root. + #[cfg(windows)] + { + let cache_root = temporary.path().join("package-cache-root"); + fs::create_dir(&cache_root).expect("cache root"); + let output = run_cli( + &data_root, + &[ + "workspace", + "set-package-cache", + &id, + path_arg(&cache_root), + "--revision", + &revision(&data_root).to_string(), + ], + ); + assert_success(&output); + } + // When the operator fetches a declared package URL. let url = fixture.url("/fixtures/pkg.bin"); let output = run_cli( @@ -187,7 +206,10 @@ fn a_trusted_fetch_writes_a_private_cache_and_is_audited() { assert_eq!(report["bytes"], BODY.len()); assert_eq!(report["status"], 200); assert_eq!(report["profile_name"], PROFILE); + #[cfg(target_os = "linux")] let cache = data_root.join("package-cache").join(&id).join("pkg.bin"); + #[cfg(windows)] + let cache = temporary.path().join("package-cache-root").join("pkg.bin"); assert_eq!(fs::read(&cache).expect("cache file"), BODY); let head = fixture.finish(); assert!(head.starts_with("GET /fixtures/pkg.bin "), "{head}"); diff --git a/crates/omgcode-cli/tests/run_workflow.rs b/crates/omgcode-cli/tests/run_workflow.rs index 419c562..6fea459 100644 --- a/crates/omgcode-cli/tests/run_workflow.rs +++ b/crates/omgcode-cli/tests/run_workflow.rs @@ -125,8 +125,24 @@ fn an_ungranted_package_cache_root_fails_the_run() { ]); assert_success(&output); fs::write(cache_root.join("pkg.bin"), b"configured-cache").expect("cache file"); - let output = scene.run("system", &[], &shell("echo should-not-run")); + let output = scene.run("system", &[], &shell("echo ran>ran.txt")); assert_error_code(&output, "run"); + assert!( + !scene.workspace.join("ran.txt").exists(), + "the refused run started its program" + ); + let audit = scene.audit(); + let finished: Vec<&Value> = audit["entries"] + .as_array() + .expect("entries") + .iter() + .filter(|entry| entry["action"]["type"] == "run_finished") + .collect(); + assert_eq!(finished.len(), 1, "{audit}"); + assert_eq!( + finished[0]["action"]["payload"]["finish"]["result"]["kind"], "not_started", + "{audit}" + ); scene.close(); } diff --git a/crates/omgcode-core/src/control.rs b/crates/omgcode-core/src/control.rs index 129294a..33305e1 100644 --- a/crates/omgcode-core/src/control.rs +++ b/crates/omgcode-core/src/control.rs @@ -586,6 +586,14 @@ impl ControlStore { }) { return Ok(()); } + self.refuse_foreign_cache_grant(workspace_id, &directory)?; + if omgcode_platform::container::CONTAINER_GRANTS_SUPPORTED + && let Some(previous) = self.package_cache_root(workspace_id).cloned() + { + for grant in self.cache_root_grants(workspace_id, &previous)? { + self.revoke_container_grant(grant)?; + } + } self.append( Action::PackageCacheRootSet { workspace_id, @@ -607,13 +615,13 @@ impl ControlStore { if self.workspace(workspace_id).is_none() { return Err(ControlError::UnknownWorkspace); } - if !self - .snapshot - .package_cache_roots - .iter() - .any(|existing| existing.workspace_id == workspace_id) - { + let Some(previous) = self.package_cache_root(workspace_id).cloned() else { return Ok(()); + }; + if omgcode_platform::container::CONTAINER_GRANTS_SUPPORTED { + for grant in self.cache_root_grants(workspace_id, &previous)? { + self.revoke_container_grant(grant)?; + } } self.append( Action::PackageCacheRootCleared { workspace_id }, @@ -622,6 +630,59 @@ impl ControlStore { Ok(()) } + /// Read-execute grants of this workspace that cover `root`, or the cache + /// itself, in either direction. A cache-root transition must revoke them + /// before the new selection takes effect. + fn cache_root_grants( + &self, + workspace_id: WorkspaceId, + root: &DirectoryRecord, + ) -> Result, ControlError> { + let mut grants = Vec::new(); + for status in self.container_grants() { + let grant = status.grant(); + if grant.workspace_id() != workspace_id + || grant.access() != ContainerGrantAccess::ReadExecute + { + continue; + } + let directory = grant.directory(); + if directory.same_directory(root) + || directory.contains(root)? + || root.contains(directory)? + { + grants.push(grant.id()); + } + } + Ok(grants) + } + + /// Refuse a root that another workspace's container can already read. + fn refuse_foreign_cache_grant( + &self, + workspace_id: WorkspaceId, + root: &DirectoryRecord, + ) -> Result<(), ControlError> { + for status in self.container_grants() { + let grant = status.grant(); + if grant.workspace_id() == workspace_id + || grant.access() != ContainerGrantAccess::ReadExecute + { + continue; + } + let directory = grant.directory(); + if directory.same_directory(root) + || directory.contains(root)? + || root.contains(directory)? + { + return Err(ControlError::PackageCacheRefused( + "another workspace's container already has access to this directory", + )); + } + } + Ok(()) + } + /// The selected package cache root for one workspace, if any. pub(crate) fn package_cache_root(&self, workspace_id: WorkspaceId) -> Option<&DirectoryRecord> { self.snapshot @@ -1224,6 +1285,43 @@ mod tests { )); } + /// A cache-root change must find grants that covered the old root and + /// refuse a new root another workspace can already read. + #[test] + fn cache_root_changes_select_covering_grants_and_refuse_foreign_access() { + let directory = tempfile::tempdir().expect("private temporary directory"); + let data = directory.path().join("data"); + let root = directory.path().join("projects"); + fs::create_dir_all(root.join("first")).expect("development root"); + fs::create_dir_all(root.join("second")).expect("development root"); + let mut store = ControlStore::open(&data).expect("control store"); + store.set_development_root(&root, 0).expect("root"); + let first = store + .register_workspace(&root.join("first"), 1) + .expect("first workspace") + .id(); + let second = store + .register_workspace(&root.join("second"), store.revision()) + .expect("second workspace") + .id(); + let cache = directory.path().join("cache"); + fs::create_dir(&cache).expect("cache root"); + let nested = cache.join("nested"); + fs::create_dir(&nested).expect("nested cache root"); + let foreign = store.test_record_grant(second, &cache, ContainerGrantAccess::ReadExecute); + let nested_record = DirectoryRecord::capture(&nested).expect("nested record"); + assert!(matches!( + store.refuse_foreign_cache_grant(first, &nested_record), + Err(ControlError::PackageCacheRefused(_)) + )); + let own = store.test_record_grant(first, &cache, ContainerGrantAccess::ReadExecute); + let grants = store + .cache_root_grants(first, &nested_record) + .expect("covering grants"); + assert!(grants.contains(&own), "{grants:?}"); + assert!(!grants.contains(&foreign), "{grants:?}"); + } + /// A failed write must leave acknowledged state unchanged and disable the writer. #[test] fn write_failure_preserves_state_and_poisons_future_settings() { diff --git a/crates/omgcode-core/src/control/approval.rs b/crates/omgcode-core/src/control/approval.rs index 7a2b6ca..1781737 100644 --- a/crates/omgcode-core/src/control/approval.rs +++ b/crates/omgcode-core/src/control/approval.rs @@ -191,7 +191,7 @@ impl ControlStore { .snapshot .approvals .iter() - .filter(|record| record.decision.is_none()) + .filter(|record| pending_at(record, now_unix)) .count() >= MAX_PENDING_APPROVALS { @@ -324,6 +324,13 @@ impl ControlStore { } } +/// Whether a record still occupies a pending slot at `now`. An expired, +/// undecided record keeps its history but cannot be decided, so it must not +/// block a new request. +fn pending_at(record: &ApprovalRecord, now: u64) -> bool { + record.decision.is_none() && record.request.expires_unix > now +} + /// Validate and apply one approval action during replay or append preflight. pub(super) fn apply( state: &mut ControlSnapshot, @@ -346,7 +353,7 @@ pub(super) fn apply( || state .approvals .iter() - .filter(|record| record.decision.is_none()) + .filter(|record| pending_at(record, request.created_unix)) .count() >= MAX_PENDING_APPROVALS { diff --git a/crates/omgcode-core/src/control/approval/tests.rs b/crates/omgcode-core/src/control/approval/tests.rs index 156ee92..659f6de 100644 --- a/crates/omgcode-core/src/control/approval/tests.rs +++ b/crates/omgcode-core/src/control/approval/tests.rs @@ -135,6 +135,33 @@ fn a_denied_or_expired_approval_cannot_be_consumed() { )); } +#[test] +fn expired_undecided_requests_do_not_fill_the_pending_limit() { + let (_directory, data, workspace_id, mut store) = scene(); + let session_id = Uuid::new_v4(); + let mut short = declaration(workspace_id, session_id); + short.ttl_secs = MIN_APPROVAL_TTL_SECS; + for offset in 0..MAX_PENDING_APPROVALS { + store + .request_approval(&short, NOW + offset as u64) + .expect("short request"); + } + let later = NOW + MIN_APPROVAL_TTL_SECS + MAX_PENDING_APPROVALS as u64; + let approval = store + .request_approval(&declaration(workspace_id, session_id), later) + .expect("a new request after every pending slot expired"); + assert_eq!(approval.state, ApprovalState::Pending); + drop(store); + let store = ControlStore::open(&data).expect("expired approvals replay"); + assert_eq!( + store + .approval_status(approval.id, later) + .expect("status") + .state, + ApprovalState::Pending + ); +} + #[test] fn binding_and_settings_changes_refuse_consumption() { let (_directory, _data, workspace_id, mut store) = scene(); diff --git a/crates/omgcode-core/src/control/profile.rs b/crates/omgcode-core/src/control/profile.rs index 9c1c131..96196de 100644 --- a/crates/omgcode-core/src/control/profile.rs +++ b/crates/omgcode-core/src/control/profile.rs @@ -234,7 +234,17 @@ impl ControlStore { .collect(), }; // Replay validation also runs in the append preflight; check the - // shape first so the operator gets the specific reason. + // shape first so the operator gets the specific reason. A reserved + // runner variable is refused only for new declarations: a profile + // recorded before the reserve existed must remain readable, and the + // runner skips its value when building the environment. + for (name, _) in &declaration.environment { + if is_reserved_variable(name) { + return Err(ControlError::ProfileRefused( + "this variable is set by the runner and cannot be declared", + )); + } + } validate_shape(&profile)?; self.append( Action::ProfileAdded { @@ -357,11 +367,6 @@ fn validate_shape(profile: &ToolchainProfile) -> Result<(), ControlError> { "variable names are ASCII letters, digits, and '_', not starting with a digit", )); } - if is_reserved_variable(&variable.name) { - return Err(ControlError::ProfileRefused( - "this variable is set by the runner and cannot be declared", - )); - } if variable.value.len() > MAX_VARIABLE_VALUE_BYTES || variable.value.contains('\0') { return Err(ControlError::ProfileRefused( "variable values are at most 4096 bytes and contain no NUL", diff --git a/crates/omgcode-core/src/control/profile/tests.rs b/crates/omgcode-core/src/control/profile/tests.rs index 16399cf..5b08e52 100644 --- a/crates/omgcode-core/src/control/profile/tests.rs +++ b/crates/omgcode-core/src/control/profile/tests.rs @@ -216,10 +216,6 @@ fn replay_refuses_inconsistent_profile_records() { "no roots", Box::new(|profile| profile.read_only_roots.clear()), ), - ( - "reserved variable", - Box::new(|profile| profile.environment[0].name = "Path".into()), - ), ("nil id", Box::new(|profile| profile.id = Uuid::nil())), ]; for (label, mutate) in mutations { @@ -253,6 +249,49 @@ fn replay_refuses_inconsistent_profile_records() { assert!(ControlStore::open(&scene.data).is_err(), "unknown removal"); } +#[test] +fn a_new_profile_cannot_declare_a_runner_variable() { + let mut scene = scene(); + let mut declaration = declaration(&scene); + declaration + .environment + .push(("OMGCODE_PACKAGE_CACHE".into(), "legacy".into())); + assert!(matches!( + scene.store.add_profile(&declaration, 1), + Err(ControlError::ProfileRefused(_)) + )); +} + +#[test] +fn a_legacy_profile_with_a_runner_variable_still_replays() { + let mut scene = scene(); + scene + .store + .add_profile(&declaration(&scene), 1) + .expect("profile"); + let entries = scene.store.entries.clone(); + drop(scene.store); + let mut legacy = entries; + let last = legacy.last_mut().expect("profile event"); + let Action::ProfileAdded { profile } = &mut last.action else { + panic!("expected a profile event"); + }; + profile.environment.push(ProfileVariable { + name: "OMGCODE_PACKAGE_CACHE".into(), + value: "legacy-value".into(), + }); + write_entries(&scene.data, &legacy); + let store = ControlStore::open(&scene.data).expect("legacy profile replays"); + let profile = store.profile("tools").expect("profile"); + assert!( + profile + .profile() + .environment() + .iter() + .any(|variable| variable.name() == "OMGCODE_PACKAGE_CACHE") + ); +} + #[test] fn reserved_names_and_profile_names_are_checked_case_insensitively() { for name in RESERVED_VARIABLES { diff --git a/crates/omgcode-core/src/packages.rs b/crates/omgcode-core/src/packages.rs index 240ae40..5edf4da 100644 --- a/crates/omgcode-core/src/packages.rs +++ b/crates/omgcode-core/src/packages.rs @@ -180,9 +180,21 @@ pub fn fetch(data_root: &Path, request: &FetchRequest) -> Result data_root - .join("package-cache") - .join(request.workspace_id.to_string()), + None => { + #[cfg(target_os = "linux")] + { + data_root + .join("package-cache") + .join(request.workspace_id.to_string()) + } + #[cfg(not(target_os = "linux"))] + { + let _ = data_root; + return Err(FetchError::Refused( + "a package cache root must be selected before fetching on this platform", + )); + } + } }; storage::create_private_directories(&cache) .map_err(|error| FetchError::Cache(io::Error::other(error.to_string())))?; @@ -304,6 +316,22 @@ fn matching_origin(profile: &PackageProfile, url: &Url) -> Result<(), FetchError Ok(()) } +/// Publish a completed private partial under a new cache name without +/// replacing an existing entry. A hard link is one atomic no-replace +/// operation in the same directory; a concurrent winner leaves this caller +/// with `AlreadyCached`. The cache is not power-loss qualified. +fn publish_cache_file(temporary: &Path, final_path: &Path, name: &str) -> Result<(), FetchError> { + fs::hard_link(temporary, final_path).map_err(|error| { + if fs::symlink_metadata(final_path).is_ok() { + FetchError::AlreadyCached(name.to_owned()) + } else { + FetchError::Cache(error) + } + })?; + let _ = fs::remove_file(temporary); + Ok(()) +} + fn perform( start: &PackageFetchStart, url: &Url, @@ -311,6 +339,7 @@ fn perform( final_path: &Path, ) -> Result<(FetchReport, u16), FailedFetch> { let timeout = Duration::from_millis(start.timeout_ms); + let deadline = Instant::now() + timeout; let client = Client::builder() .no_proxy() .redirect(Policy::none()) @@ -350,6 +379,20 @@ fn perform( let mut bytes = 0u64; let mut buffer = vec![0u8; READ_CHUNK]; loop { + // reqwest's timeout bounds each read, not the whole response. A + // server that trickles bytes must not extend the fetch indefinitely. + if Instant::now() >= deadline { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(FailedFetch { + error: FetchError::Body(io::Error::new( + io::ErrorKind::TimedOut, + "the package fetch deadline passed", + )), + reason: PackageFetchFailure::Transport, + status: Some(http_status), + }); + } let read = match response.read(&mut buffer) { Ok(0) => break, Ok(read) => read, @@ -396,7 +439,9 @@ fn perform( // The cache is private to the user; a same-user writer can already change // it. Refuse a name that appeared after the pre-check, and publish with a - // rename. No power-loss claim is made for this cache. + // hard link so two concurrent fetches cannot replace each other's file. + // The link fails atomically when the destination exists. No power-loss + // claim is made for this cache. match fs::symlink_metadata(final_path) { Ok(_) => { let _ = fs::remove_file(&temporary); @@ -416,14 +461,8 @@ fn perform( }); } } - if let Err(error) = fs::rename(&temporary, final_path) { + if let Err(error) = publish_cache_file(&temporary, final_path, &start.output_name) { let _ = fs::remove_file(&temporary); - let already = fs::symlink_metadata(final_path).is_ok(); - let error = if already { - FetchError::AlreadyCached(start.output_name.clone()) - } else { - FetchError::Cache(error) - }; return Err(FailedFetch { error, reason: PackageFetchFailure::Cache, diff --git a/crates/omgcode-core/src/packages/tests.rs b/crates/omgcode-core/src/packages/tests.rs index 6c4ab66..1a9c2f2 100644 --- a/crates/omgcode-core/src/packages/tests.rs +++ b/crates/omgcode-core/src/packages/tests.rs @@ -99,10 +99,20 @@ struct Scene { _directory: tempfile::TempDir, data: PathBuf, workspace_id: WorkspaceId, + cache_root: PathBuf, } impl Scene { fn new(origins: &[String]) -> Self { + Self::build(origins, true) + } + + #[cfg(not(target_os = "linux"))] + fn unconfigured(origins: &[String]) -> Self { + Self::build(origins, false) + } + + fn build(origins: &[String], configure_cache: bool) -> Self { let directory = tempfile::tempdir().expect("temporary directory"); let data = directory.path().join("data"); let root = directory.path().join("dev"); @@ -130,10 +140,18 @@ impl Scene { store.revision(), ) .expect("profile"); + let cache_root = directory.path().join("cache"); + if configure_cache && !cfg!(target_os = "linux") { + fs::create_dir(&cache_root).expect("cache root"); + store + .set_package_cache_root(workspace_id, &cache_root, store.revision()) + .expect("cache root"); + } Self { _directory: directory, data, workspace_id, + cache_root, } } @@ -149,10 +167,14 @@ impl Scene { } fn cache_file(&self, name: &str) -> PathBuf { - self.data - .join("package-cache") - .join(self.workspace_id.to_string()) - .join(name) + if cfg!(target_os = "linux") { + self.data + .join("package-cache") + .join(self.workspace_id.to_string()) + .join(name) + } else { + self.cache_root.join(name) + } } fn audit_text(&self) -> String { @@ -342,3 +364,143 @@ fn task_network_is_required_and_bad_names_are_refused() { assert!(matches!(error, FetchError::Refused(_)), "{name}: {error}"); } } + +#[cfg(not(target_os = "linux"))] +#[test] +fn a_fetch_without_a_cache_root_is_refused_off_linux() { + let scene = Scene::unconfigured(&["http://127.0.0.1:9/fixtures/".to_owned()]); + let before = scene.audit_text(); + let error = fetch( + &scene.data, + &scene.request( + "http://127.0.0.1:9/fixtures/pkg.bin".to_owned(), + "pkg.bin", + 1024, + ), + ) + .expect_err("no cache root is available"); + assert!(matches!(error, FetchError::Refused(_)), "{error}"); + assert_eq!(scene.audit_text(), before); + assert!(!scene.cache_file("pkg.bin").exists()); +} + +#[test] +fn concurrent_publication_never_replaces_the_winner() { + use std::sync::{Arc, Barrier}; + + let directory = tempfile::tempdir().expect("temporary directory"); + let first = directory.path().join("first.partial"); + let second = directory.path().join("second.partial"); + fs::write(&first, b"first").expect("first partial"); + fs::write(&second, b"second").expect("second partial"); + let final_path = directory.path().join("pkg.bin"); + let barrier = Arc::new(Barrier::new(2)); + let results: Vec> = std::thread::scope(|scope| { + let handles: Vec<_> = [&first, &second] + .into_iter() + .map(|temporary| { + let barrier = Arc::clone(&barrier); + let final_path = final_path.clone(); + scope.spawn(move || { + barrier.wait(); + publish_cache_file(temporary, &final_path, "pkg.bin") + }) + }) + .collect(); + handles + .into_iter() + .map(|handle| handle.join().expect("publisher")) + .collect() + }); + assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1); + assert_eq!( + results + .iter() + .filter(|result| matches!(result, Err(FetchError::AlreadyCached(_)))) + .count(), + 1 + ); + let content = fs::read(&final_path).expect("published file"); + assert!( + content == b"first" || content == b"second", + "the final file is one complete payload" + ); +} + +/// A server that sends one body byte at a time, each inside a read timeout. +struct TrickleFixture { + address: SocketAddr, + task: JoinHandle<()>, +} + +impl TrickleFixture { + fn start(interval: Duration, bytes: usize) -> Self { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind fixture"); + listener + .set_nonblocking(true) + .expect("make fixture accept bounded"); + let address = listener.local_addr().expect("fixture address"); + let task = thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(5); + let (mut stream, _) = loop { + match listener.accept() { + Ok(connection) => break connection, + Err(error) if error.kind() == io::ErrorKind::WouldBlock => { + assert!(Instant::now() < deadline, "fixture accept timed out"); + thread::sleep(Duration::from_millis(2)); + } + Err(error) => panic!("fixture accept failed: {error}"), + } + }; + stream + .set_nonblocking(false) + .expect("make accepted stream blocking"); + let head = read_head(&mut stream); + assert!(head.starts_with("GET "), "{head}"); + let response = + format!("HTTP/1.1 200 OK\r\nContent-Length: {bytes}\r\nConnection: close\r\n\r\n"); + if stream.write_all(response.as_bytes()).is_err() { + return; + } + for _ in 0..bytes { + if stream.write_all(b"x").is_err() { + return; + } + let _ = stream.flush(); + thread::sleep(interval); + } + }); + Self { address, task } + } + + fn url(&self, path: &str) -> String { + format!("http://{}{path}", self.address) + } + + fn finish(self) { + let deadline = Instant::now() + Duration::from_secs(8); + while !self.task.is_finished() { + assert!(Instant::now() < deadline, "fixture finished"); + thread::sleep(Duration::from_millis(2)); + } + self.task.join().expect("fixture joined"); + } +} + +#[test] +fn a_trickled_body_cannot_outlive_the_fetch_deadline() { + let fixture = TrickleFixture::start(Duration::from_millis(200), 32); + let scene = Scene::new(&[format!("http://{}/fixtures/", fixture.address)]); + let mut request = scene.request(fixture.url("/fixtures/slow.bin"), "slow.bin", 1024); + request.timeout = Duration::from_secs(1); + let started = Instant::now(); + let error = fetch(&scene.data, &request).expect_err("the overall deadline must stop the fetch"); + assert!( + started.elapsed() < Duration::from_secs(3), + "the fetch ran past its deadline: {:?}", + started.elapsed() + ); + assert!(matches!(error, FetchError::Body(_)), "{error}"); + assert!(!scene.cache_file("slow.bin").exists()); + fixture.finish(); +} diff --git a/crates/omgcode-core/src/run.rs b/crates/omgcode-core/src/run.rs index 48ed20f..230a014 100644 --- a/crates/omgcode-core/src/run.rs +++ b/crates/omgcode-core/src/run.rs @@ -85,6 +85,10 @@ pub struct RunRequest { /// `task_network` preference. On Linux the run can then reach any address, /// loopback included; on Windows only the internet. See ADR-0032. pub network: bool, + /// Whether this request came from a model tool. Such a run also requires + /// the live `model_tools` preference for its whole life; trusted CLI runs + /// leave this false and are unaffected by later model-tool changes. + pub model_tools: bool, /// Deadline, output, cleanup, and tree bounds. Tree limits are required; /// a request without them is refused before anything is recorded. pub limits: ProcessLimits, @@ -518,6 +522,17 @@ fn withdrawn(control: &ControlStore, start: &RunStart) -> Option<&'static str> { Some(status) if status.profile().id() == start.profile_id => {} _ => return Some("the toolchain profile was removed"), } + if start.model_origin && !registration.permissions().model_tools { + return Some("the workspace model_tools preference is disabled"); + } + match ( + start.package_cache_root.as_ref(), + control.package_cache_root(start.workspace_id), + ) { + (None, None) => {} + (Some(recorded), Some(current)) if recorded.same_directory(current) => {} + _ => return Some("the workspace package cache root changed"), + } let Ok(scope) = control.file_scope(start.workspace_id) else { return Some("the workspace is outside the development root"); }; @@ -809,6 +824,11 @@ fn prepare( "host-service access is not available on Windows", )); } + if request.model_tools && !permissions.model_tools { + return Err(RunError::Refused( + "the workspace model_tools preference is disabled", + )); + } permitted(permissions, request.host_services, request.network).map_err(RunError::Refused)?; let status = control .profile(&request.profile) @@ -859,8 +879,15 @@ fn prepare( &mut environment, )?; for variable in profile.environment() { + // A legacy profile may declare a runner-owned variable. The runner's + // value was already set above and takes precedence; do not let the + // profile override it. + if crate::control::is_reserved_variable(variable.name()) { + continue; + } environment.push((variable.name().into(), variable.value().into())); } + let package_cache_root = control.package_cache_root(request.workspace_id).cloned(); let start = RunStart { run_id, workspace_id: request.workspace_id, @@ -894,6 +921,8 @@ fn prepare( None }, package_cache, + package_cache_root, + model_origin: request.model_tools, }; Ok(Prepared { start, @@ -1362,6 +1391,8 @@ mod tests { limits: None, network: None, package_cache: false, + package_cache_root: None, + model_origin: false, } } @@ -1444,6 +1475,7 @@ mod tests { working_directory: None, host_services: false, network: false, + model_tools: false, limits: ProcessLimits { tree: Some(tree), ..ProcessLimits::default() @@ -1668,6 +1700,66 @@ mod tests { assert_eq!(withdrawn(&control, &networked), None); } + /// A run requested by the model also needs live `model_tools`; a trusted + /// CLI run does not. + #[cfg(target_os = "linux")] + #[test] + fn disabling_model_tools_withdraws_only_model_origin_runs() { + let directory = tempfile::tempdir().expect("temporary directory"); + let (data, workspace_id, _journal_root, _store_id) = store(directory.path()); + let plain = allowed_start(&data, directory.path(), workspace_id); + let mut model = plain.clone(); + model.model_origin = true; + let control = open_control(&data).expect("control"); + assert_eq!( + withdrawn(&control, &model), + Some("the workspace model_tools preference is disabled") + ); + assert_eq!(withdrawn(&control, &plain), None); + drop(control); + let mut control = ControlStore::open(&data).expect("control store"); + control + .set_permissions( + workspace_id, + PermissionSet { + execute: true, + model_tools: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("permissions"); + drop(control); + let control = open_control(&data).expect("control"); + assert_eq!(withdrawn(&control, &model), None); + } + + /// A configured cache-root change withdraws a run that holds the old + /// exposure. + #[cfg(target_os = "linux")] + #[test] + fn changing_the_package_cache_root_withdraws_the_run() { + let directory = tempfile::tempdir().expect("temporary directory"); + let (data, workspace_id, _journal_root, _store_id) = store(directory.path()); + let mut start = allowed_start(&data, directory.path(), workspace_id); + let cache = directory.path().join("cache"); + fs::create_dir(&cache).expect("cache root"); + start.package_cache_root = None; + let control = open_control(&data).expect("control"); + assert_eq!(withdrawn(&control, &start), None); + drop(control); + let mut control = ControlStore::open(&data).expect("control store"); + control + .set_package_cache_root(workspace_id, &cache, control.revision()) + .expect("cache root"); + drop(control); + let control = open_control(&data).expect("control"); + assert_eq!( + withdrawn(&control, &start), + Some("the workspace package cache root changed") + ); + } + /// A live run that the settings still allow is not waited for. Linux /// only: on Windows the run would also need a real container grant on /// the workspace, which the CLI workflow tests cover. diff --git a/crates/omgcode-core/src/run_journal.rs b/crates/omgcode-core/src/run_journal.rs index fa2d501..1fc18be 100644 --- a/crates/omgcode-core/src/run_journal.rs +++ b/crates/omgcode-core/src/run_journal.rs @@ -26,6 +26,7 @@ use uuid::{Uuid, Version}; use crate::policy::WorkspaceId; use crate::session::NativePath; use crate::storage::{self, StorageError}; +use crate::workspace::DirectoryRecord; const FILE_NAME: &str = "runs-v1.jsonl"; const SCHEMA_VERSION: u32 = 1; @@ -184,6 +185,16 @@ pub struct RunStart { /// since 0.22.0. False and earlier runs omit it. #[serde(default, skip_serializing_if = "is_false")] pub package_cache: bool, + /// The configured package cache root the run was exposed to, since + /// 0.26.0. A cache-root change withdraws the run. Earlier runs omit it; + /// a recorded root appearing later is treated as a change. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub(crate) package_cache_root: Option, + /// Whether this run was requested by a model tool and therefore also + /// requires the live `model_tools` preference, since 0.26.0. Earlier runs + /// omit it. + #[serde(default, skip_serializing_if = "is_false")] + pub model_origin: bool, } fn is_false(value: &bool) -> bool { @@ -232,7 +243,7 @@ enum RunAction { /// Bind the journal to one control store. Always first. Created, RunStarted { - start: RunStart, + start: Box, }, RunFinished { run_id: Uuid, @@ -426,7 +437,9 @@ impl RunJournal { if self.state.pending.len() >= MAX_PENDING_RUNS { return Err(RunJournalError::TooManyPending); } - self.append(RunAction::RunStarted { start }) + self.append(RunAction::RunStarted { + start: Box::new(start), + }) } /// Record a run's outcome. `CleanupUnconfirmed` keeps it pending. @@ -455,7 +468,7 @@ impl RunJournal { self.entries .iter() .filter_map(|entry| match &entry.action { - RunAction::RunStarted { start } => Some(start), + RunAction::RunStarted { start } => Some(start.as_ref()), _ => None, }) .filter(|start| { diff --git a/crates/omgcode-core/src/run_journal/tests.rs b/crates/omgcode-core/src/run_journal/tests.rs index db94755..ad6870e 100644 --- a/crates/omgcode-core/src/run_journal/tests.rs +++ b/crates/omgcode-core/src/run_journal/tests.rs @@ -26,6 +26,8 @@ fn start(run_id: Uuid) -> RunStart { }), network: None, package_cache: false, + package_cache_root: None, + model_origin: false, } } @@ -88,7 +90,9 @@ fn limits_and_usage_are_recorded_and_checked_on_replay() { }); let run = Uuid::new_v4(); let entries = [ - RunAction::RunStarted { start: start(run) }, + RunAction::RunStarted { + start: Box::new(start(run)), + }, RunAction::RunFinished { run_id: run, finish: not_started, @@ -104,7 +108,13 @@ fn limits_and_usage_are_recorded_and_checked_on_replay() { assert!(matches!( RunJournal::apply( &mut state.clone(), - &entry(store, 1, RunAction::RunStarted { start: zero }), + &entry( + store, + 1, + RunAction::RunStarted { + start: Box::new(zero), + } + ), 1, store ), @@ -157,7 +167,13 @@ fn network_access_is_recorded_and_bounded_on_replay() { assert!(matches!( RunJournal::apply( &mut ReplayState::default(), - &entry(store, 1, RunAction::RunStarted { start: excessive }), + &entry( + store, + 1, + RunAction::RunStarted { + start: Box::new(excessive), + } + ), 1, store ), diff --git a/crates/omgcode-core/src/turn.rs b/crates/omgcode-core/src/turn.rs index 76138c0..2d6cb3d 100644 --- a/crates/omgcode-core/src/turn.rs +++ b/crates/omgcode-core/src/turn.rs @@ -1348,6 +1348,7 @@ impl WorkspaceToolDispatcher<'_> { working_directory: cwd.as_ref().map(PathBuf::from), host_services: false, network: false, + model_tools: true, limits: command_limits(), }; let digest = command_digest(self.session_id, &request); @@ -1567,10 +1568,11 @@ fn command_summary(profile: &str, program: &str, arguments: usize) -> String { .unwrap_or(program); let mut summary = format!("run {profile}/{label} ({arguments} arguments)"); if summary.len() > 180 { - summary.truncate(180); - while !summary.is_char_boundary(summary.len()) { - summary.pop(); + let mut end = 180; + while !summary.is_char_boundary(end) { + end -= 1; } + summary.truncate(end); } summary } @@ -1741,3 +1743,17 @@ fn validate_relative_path(path: &str, allow_empty: bool) -> Result<(), ToolCallE } Ok(()) } + +#[cfg(test)] +mod summary_tests { + use super::command_summary; + + #[test] + fn a_command_summary_truncates_on_a_utf8_boundary() { + let program = format!("{}{}", "a".repeat(168), 'é'); + let summary = command_summary("system", &program, 3); + assert!(summary.len() <= 180); + assert!(summary.is_char_boundary(summary.len())); + assert!(summary.starts_with("run system/")); + } +} diff --git a/crates/omgcode-core/tests/control_store.rs b/crates/omgcode-core/tests/control_store.rs index c9cfdb3..fea634c 100644 --- a/crates/omgcode-core/tests/control_store.rs +++ b/crates/omgcode-core/tests/control_store.rs @@ -542,12 +542,14 @@ fn package_cache_root_is_recorded_replayed_cleared_and_scoped() { store .set_package_cache_root(workspace_id, &cache_root, revision) .expect("cache root"); - assert!( - serde_json::to_value(store.snapshot()) - .expect("snapshot") - .get("package_cache_roots") - .is_some() - ); + let recorded = serde_json::to_value(store.snapshot()).expect("snapshot"); + let recorded_root = recorded["package_cache_roots"] + .as_array() + .expect("cache roots") + .first() + .expect("selected root") + .clone(); + assert_eq!(recorded_root["workspace_id"], workspace_id.to_string()); let revision = store.revision(); assert!(matches!( store.set_package_cache_root(workspace_id, &inside, revision), @@ -570,11 +572,20 @@ fn package_cache_root_is_recorded_replayed_cleared_and_scoped() { drop(store); let mut store = ControlStore::open(&fixture.data).expect("replay"); - assert!( - serde_json::to_value(store.snapshot()) - .expect("snapshot") - .get("package_cache_roots") - .is_some() + let replayed = serde_json::to_value(store.snapshot()).expect("snapshot"); + let replayed_root = replayed["package_cache_roots"] + .as_array() + .expect("cache roots") + .first() + .expect("selected root") + .clone(); + assert_eq!( + replayed_root["workspace_id"], recorded_root["workspace_id"], + "replay changed the workspace mapping" + ); + assert_eq!( + replayed_root["directory"], recorded_root["directory"], + "replay changed the selected directory or its identity" ); let revision = store.revision(); store diff --git a/crates/omgcode-platform/src/process/disk.rs b/crates/omgcode-platform/src/process/disk.rs index 86f9615..9167ad7 100644 --- a/crates/omgcode-platform/src/process/disk.rs +++ b/crates/omgcode-platform/src/process/disk.rs @@ -37,6 +37,9 @@ const STOP_CHECK: Duration = Duration::from_millis(100); /// Most entries one sample may examine. A larger tree fails closed because the /// supervisor cannot finish the measurement it started. const MAX_SAMPLED_ENTRIES: usize = 4_000_000; +/// Longest one scan may run. A slow filesystem fails the measurement rather +/// than delaying cancellation or the final outcome without a bound. +const MAX_SCAN_TIME: Duration = Duration::from_secs(30); /// Disk usage measured above the run's baseline. pub(super) struct DiskMonitor { @@ -117,7 +120,7 @@ impl DiskMonitor { "a disk limit needs at least one writable path", )); } - let baseline = measure(&roots, None).map_err(ProcessError::DiskMeasurement)?; + let baseline = measure(&roots, None, None).map_err(ProcessError::DiskMeasurement)?; Ok(Some(Self { state: Arc::new(DiskState::default()), thread: None, @@ -175,7 +178,7 @@ impl DiskMonitor { allocated: self.baseline.allocated.saturating_add(self.limit), logical: self.baseline.logical.saturating_add(self.limit), }; - let measured = measure(&self.roots, Some(ceiling))?; + let measured = measure(&self.roots, Some(ceiling), None)?; self.record(&measured); Ok(()) } @@ -247,7 +250,7 @@ fn sample_until_stopped( if state.stop.load(Ordering::SeqCst) { return; } - match measure(roots, Some(ceiling)) { + match measure(roots, Some(ceiling), Some(&state.stop)) { Ok(measured) => { let growth = measured .allocated @@ -259,6 +262,11 @@ fn sample_until_stopped( return; } } + Err(_) if state.stop.load(Ordering::SeqCst) => { + // Cancellation interrupted this scan. That is not a + // measurement failure; the supervisor is already stopping. + return; + } Err(error) => { // Only the first error matters; the supervisor stops the tree. let mut slot = state.error.lock().unwrap_or_else(PoisonError::into_inner); @@ -316,9 +324,15 @@ fn disk_roots(confinement: &Confinement) -> io::Result> { Ok(outer) } -/// Sum the bytes under `roots`, stopping once a ceiling is passed. -fn measure(roots: &[PathBuf], ceiling: Option) -> io::Result { - measure_entries(roots, ceiling, MAX_SAMPLED_ENTRIES) +/// Sum the bytes under `roots`, stopping once a ceiling is passed or `stop` +/// is set. A stopped scan reports an interrupted measurement; the caller +/// must not record that as a failure while it is already stopping. +fn measure( + roots: &[PathBuf], + ceiling: Option, + stop: Option<&AtomicBool>, +) -> io::Result { + measure_entries(roots, ceiling, MAX_SAMPLED_ENTRIES, stop) } /// [`measure`] with an explicit entry budget, so the fail-closed bound can be @@ -327,13 +341,28 @@ fn measure_entries( roots: &[PathBuf], ceiling: Option, max_entries: usize, + stop: Option<&AtomicBool>, ) -> io::Result { + let stopped = || stop.is_some_and(|flag| flag.load(Ordering::SeqCst)); + let scan_deadline = Instant::now() + MAX_SCAN_TIME; let mut allocated = 0u64; let mut logical = 0u64; let mut exhausted = false; let mut examined = 0usize; let mut pending: Vec = roots.to_vec(); while let Some(path) = pending.pop() { + if stopped() { + return Err(io::Error::new( + io::ErrorKind::Interrupted, + "the disk sample was cancelled", + )); + } + if Instant::now() >= scan_deadline { + return Err(io::Error::new( + io::ErrorKind::TimedOut, + "the disk measurement exceeded its time budget", + )); + } examined = examined.saturating_add(1); if examined > max_entries { return Err(io::Error::other( @@ -356,6 +385,26 @@ fn measure_entries( Err(error) => return Err(error), }; for entry in entries { + if stopped() { + return Err(io::Error::new( + io::ErrorKind::Interrupted, + "the disk sample was cancelled", + )); + } + if Instant::now() >= scan_deadline { + return Err(io::Error::new( + io::ErrorKind::TimedOut, + "the disk measurement exceeded its time budget", + )); + } + // Reserve each queued path against the same budget before + // allocating it, so a directory with millions of children + // cannot exhaust memory before the budget check runs. + if examined.saturating_add(pending.len()).saturating_add(1) > max_entries { + return Err(io::Error::other( + "the writable roots have too many entries to measure for the disk limit", + )); + } match entry { Ok(entry) => pending.push(entry.path()), Err(error) if error.kind() == io::ErrorKind::NotFound => {} @@ -474,7 +523,7 @@ mod tests { fs::create_dir(&nested).expect("nested directory"); write_file(directory.path(), "one.txt", 4096); write_file(&nested, "two.txt", 8192); - let measured = measure(&[directory.path().to_path_buf()], None).expect("measure"); + let measured = measure(&[directory.path().to_path_buf()], None, None).expect("measure"); assert!(!measured.exceeded); assert!( measured.allocated >= 12 * 1024 && measured.logical >= 12 * 1024, @@ -492,6 +541,7 @@ mod tests { allocated: 1, logical: 1, }), + None, ) .expect("measure past the ceiling"); assert!(measured.exceeded); @@ -510,7 +560,7 @@ mod tests { { std::os::unix::fs::symlink(directory.path(), directory.path().join("loop")) .expect("symbolic link"); - let measured = measure(&[directory.path().to_path_buf()], None).expect("measure"); + let measured = measure(&[directory.path().to_path_buf()], None, None).expect("measure"); assert!(!measured.exceeded); assert!( measured.allocated >= 4096 && measured.logical >= 4096, @@ -518,7 +568,7 @@ mod tests { ); } // A root that disappears measures nothing rather than failing. - let measured = measure(&[missing], None).expect("measure a missing root"); + let measured = measure(&[missing], None, None).expect("measure a missing root"); assert_eq!(measured.allocated, 0); assert_eq!(measured.logical, 0); } @@ -550,7 +600,7 @@ mod tests { let directory = tempfile::tempdir().expect("temporary directory"); write_file(directory.path(), "one.txt", 4096); let root = directory.path().to_path_buf(); - let error = measure_entries(std::slice::from_ref(&root), None, 1) + let error = measure_entries(std::slice::from_ref(&root), None, 1, None) .expect_err("the entry budget was exceeded"); assert!(error.to_string().contains("too many entries"), "{error}"); } @@ -566,13 +616,34 @@ mod tests { write_file(&locked, "one.txt", 4096); fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)) .expect("remove permissions"); - let measured = measure(&[directory.path().to_path_buf()], None); + let measured = measure(&[directory.path().to_path_buf()], None, None); // Restore access before the temporary directory is removed. fs::set_permissions(&locked, fs::Permissions::from_mode(0o700)) .expect("restore permissions"); assert!(measured.is_err(), "an unreadable directory was measured"); } + #[test] + fn a_cancelled_measurement_reports_interruption() { + let directory = tempfile::tempdir().expect("temporary directory"); + write_file(directory.path(), "one.txt", 4096); + let stop = AtomicBool::new(true); + let error = measure(&[directory.path().to_path_buf()], None, Some(&stop)) + .expect_err("a stopped scan must not report success"); + assert_eq!(error.kind(), io::ErrorKind::Interrupted); + } + + #[test] + fn a_directory_with_more_children_than_the_budget_is_refused_before_queuing() { + let directory = tempfile::tempdir().expect("temporary directory"); + for index in 0..20 { + write_file(directory.path(), &format!("file-{index}.txt"), 1); + } + let error = measure_entries(&[directory.path().to_path_buf()], None, 5, None) + .expect_err("the entry budget was exceeded"); + assert!(error.to_string().contains("too many entries"), "{error}"); + } + #[test] fn nested_roots_are_counted_once() { let directory = tempfile::tempdir().expect("temporary directory"); @@ -585,8 +656,9 @@ mod tests { }; let roots = disk_roots(&confinement).expect("roots"); assert_eq!(roots, vec![directory.path().to_path_buf()]); - let outer = measure(&roots, None).expect("measure the outer root"); - let inner = measure(std::slice::from_ref(&nested), None).expect("measure the nested root"); + let outer = measure(&roots, None, None).expect("measure the outer root"); + let inner = + measure(std::slice::from_ref(&nested), None, None).expect("measure the nested root"); assert_eq!(outer.allocated, inner.allocated); assert_eq!(outer.logical, inner.logical); } diff --git a/docs/README.md b/docs/README.md index 7e9aade..42baeb9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,8 +6,9 @@ trusted operator commands and, when a workspace enables `model_tools`, to the local model's read, list, and approval-gated command tools. Model write, network, Git, and MCP tools are not implemented. The trusted CLI `run` command runs one program under Linux Landlock or Windows -AppContainer confinement with an operator-declared toolchain profile. No model -tool starts a process. The guides distinguish implemented behaviour from the +AppContainer confinement with an operator-declared toolchain profile. A model +tool starts a process only through the approval-gated command tool and the +trusted runner. The guides distinguish implemented behaviour from the remaining product contracts. ## Start here @@ -92,11 +93,12 @@ uses them, and network access remains all-or-nothing. The trusted `packages fetch` command downloads from declared origins into a per-workspace cache. A selected root is exposed read-only on Linux and Windows; Windows needs a read-execute grant, and Linux falls back to application storage. -Provider profiles record connection shapes, and approval records prepare -per-call model-tool consent; neither enables an adapter or model tool. The -limited file broker and the core read/list turn runtime -do not provide general model tools. Chat dispatches only the read and -list tools, and only for a workspace that enables `model_tools`. A read-only CLI preview compares a workspace file with trusted proposed +Provider profiles record connection shapes without credentials or adapters; +no adapter or login uses them. Approval records authorize one exact +model-tool call, and the command tool is their only consumer. Chat dispatches +the read/list tools and, with the required live permissions, the +approval-gated command tool, only for a workspace that enables `model_tools`. +A read-only CLI preview compares a workspace file with trusted proposed content without writing to the workspace or creating a checkpoint. Dependency security audits, native containment qualification, and release checks still need their own implementation and evidence. diff --git a/docs/adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md b/docs/adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md index 632e953..a5709d4 100644 --- a/docs/adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md +++ b/docs/adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md @@ -25,8 +25,13 @@ Add the model tool `workspace_run_command`: 2. **Arguments:** `profile`, `program`, `arguments` (at most 64 strings), and an optional workspace-relative `cwd`. Parsing is strict: unknown fields, invalid names, control characters in the program, oversized arguments, and - parent or absolute `cwd` values are refused. There is no shell; arguments - are passed exactly. + parent or absolute `cwd` values are refused. The tool has no shell mode: it + never composes or re-parses a command line, and passes every argument + exactly. The model may name any program the approved profile exposes, + including an interpreter such as `sh` or `cmd`; the interpreter's own + parsing is part of the exact program and arguments the operator approves. + A name denylist is not used because interpreters can run commands without + being named as a shell. 3. **Fixed limits:** the tool does not accept model-selected limits. It uses a 20-second deadline, 4 KiB retained from each output stream, 1 GiB memory, 64 processes, 20 seconds of CPU, and 256 MiB of disk growth. It selects no @@ -59,8 +64,10 @@ native confinement remain the authority and the only process path. more than one exact request. - **Model-selected limits or network access.** Rejected: the model cannot widen its own resource or network boundary. -- **A shell command string.** Rejected: it would re-parse untrusted text and - break exact argument handling. +- **A shell command string.** Rejected: the tool would re-parse untrusted + text and break exact argument handling. Naming an interpreter as the + program remains allowed and is covered by the operator's exact-request + approval; rejecting every interpreter by name is not a reliable boundary. - **Direct unconfined execution from the model tool.** Rejected: the trusted runner is the only process path. - **A UI approval inside the chat turn.** Deferred: chat is non-interactive diff --git a/docs/execution.md b/docs/execution.md index fc2dc8b..0c2e268 100644 --- a/docs/execution.md +++ b/docs/execution.md @@ -270,7 +270,11 @@ omgcode workspace clear-package-cache --revision `set-package-cache` selects one workspace's cache directory. It must exist, must not overlap application storage or the development root, and must not -overlap another workspace's root. `clear-package-cache` removes the selection +overlap another workspace's root. Clearing or replacing a root revokes +read-execute container grants that covered the old root, and selecting a +directory another workspace's container can already read is refused. Runs that +held the old exposure are stopped before the command completes. +`clear-package-cache` removes the selection and leaves cached files in place. Without a selected root, Linux uses the application-storage fallback and Windows has none. A 0.23.0 control journal cannot be read by 0.22.0. @@ -291,12 +295,16 @@ variables are ignored, no credentials are sent, and nothing is retried. TLS uses the operating-system trust store through `reqwest`'s `native-tls` feature; certificate validation is never disabled. The response must be 2xx and its body is streamed, hashed with SHA-256, and stopped if it passes the -byte limit. +byte limit. The overall deadline is checked before each read, so a server that +trickles bytes cannot extend the fetch past its requested timeout. The file is written as a private `.partial-` entry, synced, and -renamed into the selected workspace cache root, or into -`/package-cache//` when no root is selected. Existing -names are never overwritten. The cache makes no power-loss claim. The control +published with an atomic no-replace hard link into the selected workspace +cache root, or into `/package-cache//` on Linux when no +root is selected. A fetch with no selected root is refused on Windows before +anything is recorded. Existing names are never overwritten; two concurrent +fetches cannot replace each other, and the loser reports already-cached. The +cache makes no power-loss claim. The control journal records `package_fetch_started` before the request and `package_fetch_finished` after it; both preserve the settings revision. They record the profile identity and revision, a SHA-256 of the URL (never its @@ -651,8 +659,11 @@ runtime advertises it only when the session workspace's live `model_tools`, them, and the trusted run checks them again. Arguments are a recorded toolchain `profile`, a `program`, at most 64 -`arguments`, and an optional workspace-relative `cwd`. There is no shell. -The tool fixes its limits: a 20-second deadline, 4 KiB retained from each +`arguments`, and an optional workspace-relative `cwd`. The tool has no shell +mode: it never composes or re-parses a command line. The model may name any +program the approved profile exposes, including an interpreter such as `sh` or +`cmd`; that program's own parsing is part of the exact request the operator +approves. The tool fixes its limits: a 20-second deadline, 4 KiB retained from each output stream, 1 GiB memory, 64 processes, 20 seconds of CPU, and 256 MiB of disk growth. It selects no network or host services. diff --git a/docs/security-coding-rules.md b/docs/security-coding-rules.md index 0aa9787..ccdac28 100644 --- a/docs/security-coding-rules.md +++ b/docs/security-coding-rules.md @@ -239,8 +239,11 @@ crate. `model_tools`, `execute`, `read`, and `write`. Its first call records a pending approval and starts nothing; only a later exact call may consume an approved record and call the trusted runner. The model cannot approve, - select limits, use a shell, select network or host services, or reuse an - approval. Treat its output as untrusted. See ADR-0040. + select limits, select network or host services, or reuse an approval. It + cannot ask the tool to compose or re-parse a shell command line; it may + name a program the approved profile exposes, including an interpreter, and + the operator's exact-request approval covers that program and its + arguments. Treat its output as untrusted. See ADR-0040. - Do not enforce a package or task destination list with proxy environment variables or an advisory loopback proxy: a confined tree can ignore them and connect directly. A package-network profile is configuration only until a diff --git a/docs/testing-standards.md b/docs/testing-standards.md index bc7b11e..a8705fb 100644 --- a/docs/testing-standards.md +++ b/docs/testing-standards.md @@ -143,7 +143,7 @@ As the relevant components are implemented, include: | Processes | Descendant escape attempts, limits, cleanup, startup failure | | Disk limits | Sampled growth ends the whole run as `disk`, baseline excludes prior content, links and reparse points are not followed, the ceiling stops a scan early, unreadable or oversized roots fail closed | | Networking | Disabled egress, host-local access, redirects, provider routing | -| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one. No run accepts a profile. A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | +| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, an overall deadline under a trickled body, atomic no-replace publication under a concurrent collision, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, grant revocation when a root changes or clears, refusal of a root another workspace can already read, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one or without a selected root. No run accepts a profile. A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | | Providers | Partial/malformed streams, duplicate calls, interruption, retries | | Provider profiles | Kind/authentication pairing, duplicate and invalid names, case-insensitive lookup, revisioned add/remove, replay, no credential or endpoint storage, and capability reporting. No adapter or login is exercised | | Approvals | Request validation, pending/list/show, approve/deny, single-use consume, expiry, binding mismatch, settings-revision invalidation, replay, unknown IDs, and limits. No operation executes | @@ -154,12 +154,12 @@ As the relevant components are implemented, include: | Frontend | Untrusted rendering, IPC authorization, no secret disclosure | | Sessions | Concurrent edits/worktrees, JSONL naming/schema/durability, index rebuild, archive/restore, safe resume, v1/v2 boundaries | | Turn/tool lifecycle | Fresh bindings, strict schemas, unknown/malformed/duplicate calls, round/call/byte limits, cancellation, deadline, source labels, denial at the broker, no replay | -| Process-tree runner | Exit codes, exact environment, argument quoting, bounded output, deadline, cancellation, descendant escape attempts, inherited handles, observed cleanup, invalid specifications | +| Process-tree runner | Exit codes, exact environment, argument quoting, bounded output, deadline, cancellation, descendant escape attempts, inherited handles, observed cleanup, cancellable disk sampling, bounded traversal queues, invalid specifications | | Linux confinement | Read-only and read-write rules, path truncation, denied host files and directories, inheritance by descendants, IP and `io_uring` denial in both Unix-socket modes, Unix-socket and datagram-pair denial in `PairsOnly`, host Unix-socket connection, workspace socket binding, and datagram pairs in `HostServices`, unexecutable programs, missing/link/special confinement paths, ABI selection below and above the minimum | | Windows confinement | Granted read-only and read-write paths, denied host files, directories, named pipe, and loopback TCP, a writable container temporary directory, inheritance by descendants, the same probes unconfined, missing, weaker, and foreign grants, a missing profile, `LOCALAPPDATA`, or container name, refused `HostServices`, an ungranted program | | Container grants | SID derivation, propagation to existing and new children, exact read-back, protection state kept, junctions not followed, hard-link reach, refused replaced/junction/file/relative paths, profile creation and deletion, journal intent and outcome order, replay validation, revision rules, scope refusals, pending and revoking recovery, the revocation reserve, CLI grant/list/revoke, refusal off Windows | | Model tool chat | Preference off by default and rechecked per call, settings compatibility, confinement against parent, absolute, rooted, UNC, link, junction, and hard-link paths, strict provider tool-call parsing, token removal before journal and model, refusal of token-bearing calls, withheld replay after revocation, finish reason, one call per turn in escape cases | -| Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, and no replay. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | +| Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, no replay, and revoking `model_tools` stops a live model-origin run. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | | MCP | Version/transport compatibility, authorization, untrusted content, local confinement, OAuth/redirects, cancellation | | LM Studio | Model discovery, token/no-token profiles, streamed tools, context limits, unavailable models, no hosted-tool bypass | | Instructions | Compiled base/UI/request match, runtime replacement denial, role-injection attempts, preference/task precedence, revisions, safe rendering | From 6abca321cf4a1080e7389857288ed9e406be97f2 Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sat, 14 Nov 2026 23:28:09 +0000 Subject: [PATCH 2/3] fix: address remaining PR #31 review findings Refuse cache-root selection when a foreign grant record is stale, and revoke a stale owner grant fail-closed. Record model_tools true-to-false changes in a durable per-workspace count that a model-origin watcher compares, so a re-enable cannot hide the revocation. Bound the package-fetch send and every body chunk by the remaining deadline with the async client and a current-thread runtime, and reject completion after expiry. Preserve real disk-scan errors that race with shutdown while ignoring only our cancellation. Add regressions for each case. Co-Authored-By: Claude Code --- CHANGELOG.md | 13 +- Cargo.lock | 1 + Cargo.toml | 1 + crates/omgcode-core/Cargo.toml | 1 + crates/omgcode-core/src/control.rs | 78 ++++++ crates/omgcode-core/src/packages.rs | 269 +++++++++++--------- crates/omgcode-core/src/packages/tests.rs | 3 +- crates/omgcode-core/src/run.rs | 54 +++- crates/omgcode-platform/src/process/disk.rs | 38 ++- docs/execution.md | 15 +- docs/testing-standards.md | 6 +- 11 files changed, 333 insertions(+), 146 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaac690..1e5660a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -347,8 +347,17 @@ Changes are recorded here before a release version is assigned. its own cache path wins. - Windows package fetch now requires a selected cache root, matching the runner's lack of an application-storage fallback. - - Package fetch checks its overall deadline before each body read, so a - trickled response cannot outlive the requested timeout. + - Package fetch checks its overall deadline before each body read, and the + send and every chunk read are bounded by the remaining time. A body that + completes after the deadline is rejected. + - A stale foreign cache-grant record refuses a cache-root selection, and a + stale owner grant is revoked fail-closed instead of being treated as + unrelated. + - A `model_tools` true-to-false change records a durable per-workspace + count. A model-origin run stops even if the preference is re-enabled + before its watcher reads the setting. + - Disk sampling preserves a real measurement error that races with + shutdown; only its own cancellation is ignored. - Package cache publication uses an atomic no-replace hard link. Two concurrent fetches cannot overwrite each other, and a synchronized collision test covers the winner. diff --git a/Cargo.lock b/Cargo.lock index 77c9829..335540e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -696,6 +696,7 @@ dependencies = [ "tempfile", "thiserror", "time", + "tokio", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index 22f94ff..4339062 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ serde_json = "1" uuid = { version = "1", features = ["v4", "serde"] } time = { version = "0.3", features = ["formatting", "parsing", "serde"] } thiserror = "2" +tokio = { version = "1", default-features = false, features = ["rt", "time", "net"] } reqwest = { version = "0.13.5", default-features = false, features = ["blocking", "json", "native-tls"] } sha2 = "0.11.1" clap = { version = "4.6.7", features = ["derive"] } diff --git a/crates/omgcode-core/Cargo.toml b/crates/omgcode-core/Cargo.toml index 11ab190..eb29888 100644 --- a/crates/omgcode-core/Cargo.toml +++ b/crates/omgcode-core/Cargo.toml @@ -14,6 +14,7 @@ uuid.workspace = true time.workspace = true thiserror.workspace = true reqwest.workspace = true +tokio.workspace = true sha2.workspace = true [dev-dependencies] diff --git a/crates/omgcode-core/src/control.rs b/crates/omgcode-core/src/control.rs index 33305e1..6903db5 100644 --- a/crates/omgcode-core/src/control.rs +++ b/crates/omgcode-core/src/control.rs @@ -214,6 +214,11 @@ pub struct ControlSnapshot { /// it with the count when their run started. #[serde(skip)] grant_revocations: BTreeMap, + /// `model_tools` true-to-false changes per workspace since the journal + /// was created. Derived on replay and never serialized; run watchers + /// compare it with the count when their run started. + #[serde(skip)] + model_tool_revocations: BTreeMap, } /// One workspace's selected fetched-package cache root. @@ -524,6 +529,7 @@ impl ControlStore { provider_profiles: Vec::new(), approvals: Vec::new(), grant_revocations: BTreeMap::new(), + model_tool_revocations: BTreeMap::new(), }, entries: Vec::new(), bytes: 0, @@ -541,6 +547,16 @@ impl ControlStore { pub const fn revision(&self) -> u64 { self.snapshot.revision } + /// How many `model_tools` true-to-false changes this workspace has + /// recorded. A model-origin run compares this with the count at start so + /// a disable followed by a re-enable still stops it. + pub(crate) fn model_tool_revocations(&self, workspace_id: WorkspaceId) -> u64 { + self.snapshot + .model_tool_revocations + .get(&workspace_id) + .copied() + .unwrap_or(0) + } /// Whether the user has explicitly selected a development root. pub const fn has_development_root(&self) -> bool { self.snapshot.development_root.is_some() @@ -647,6 +663,13 @@ impl ControlStore { continue; } let directory = grant.directory(); + if !directory.is_current()? { + // The recorded path may have moved with the ACL still + // attached. Include it so revocation fails closed rather + // than silently leaving access behind. + grants.push(grant.id()); + continue; + } if directory.same_directory(root) || directory.contains(root)? || root.contains(directory)? @@ -671,6 +694,14 @@ impl ControlStore { continue; } let directory = grant.directory(); + if !directory.is_current()? { + // A stale record may describe a directory that was renamed + // and still carries this container's ACL. We cannot prove + // separation, so refuse the selection. + return Err(ControlError::PackageCacheRefused( + "a foreign container grant record is stale; revoke it before selecting a cache root", + )); + } if directory.same_directory(root) || directory.contains(root)? || root.contains(directory)? @@ -1124,6 +1155,12 @@ impl ControlStore { .iter_mut() .find(|workspace| workspace.id == *workspace_id) .ok_or(ControlError::Invalid("unknown permission workspace"))?; + if workspace.permissions.model_tools && !permissions.model_tools { + *state + .model_tool_revocations + .entry(*workspace_id) + .or_default() += 1; + } workspace.permissions = *permissions; } Action::PolicyChecked { @@ -1322,6 +1359,47 @@ mod tests { assert!(!grants.contains(&foreign), "{grants:?}"); } + /// A renamed directory keeps its ACL, so a stale foreign grant cannot + /// prove separation and must refuse the selection. + #[test] + fn a_stale_foreign_grant_refuses_a_cache_root_selection() { + let directory = tempfile::tempdir().expect("private temporary directory"); + let data = directory.path().join("data"); + let root = directory.path().join("projects"); + fs::create_dir_all(root.join("first")).expect("development root"); + fs::create_dir_all(root.join("second")).expect("development root"); + let mut store = ControlStore::open(&data).expect("control store"); + store.set_development_root(&root, 0).expect("root"); + let first = store + .register_workspace(&root.join("first"), 1) + .expect("first workspace") + .id(); + let second = store + .register_workspace(&root.join("second"), store.revision()) + .expect("second workspace") + .id(); + let cache = directory.path().join("cache"); + fs::create_dir(&cache).expect("cache root"); + let own = store.test_record_grant(first, &cache, ContainerGrantAccess::ReadExecute); + let _foreign = store.test_record_grant(second, &cache, ContainerGrantAccess::ReadExecute); + let moved = directory.path().join("moved-cache"); + fs::rename(&cache, &moved).expect("move granted cache"); + let selected = moved.join("selected"); + fs::create_dir(&selected).expect("selected root"); + let selected_record = DirectoryRecord::capture(&selected).expect("selected record"); + assert!(matches!( + store.refuse_foreign_cache_grant(first, &selected_record), + Err(ControlError::PackageCacheRefused(_)) + )); + let grants = store + .cache_root_grants(first, &selected_record) + .expect("covering grants"); + assert!( + grants.contains(&own), + "a stale owner grant must be revoked: {grants:?}" + ); + } + /// A failed write must leave acknowledged state unchanged and disable the writer. #[test] fn write_failure_preserves_state_and_poisons_future_settings() { diff --git a/crates/omgcode-core/src/packages.rs b/crates/omgcode-core/src/packages.rs index 5edf4da..abac965 100644 --- a/crates/omgcode-core/src/packages.rs +++ b/crates/omgcode-core/src/packages.rs @@ -13,13 +13,13 @@ use std::fmt::Write as _; use std::fs::{self, OpenOptions}; -use std::io::{self, Read, Write}; +use std::io::{self, Write}; use std::path::Path; use std::thread; use std::time::{Duration, Instant}; +use reqwest::Client; use reqwest::Url; -use reqwest::blocking::Client; use reqwest::redirect::Policy; use serde::Serialize; use sha2::{Digest, Sha256}; @@ -48,7 +48,6 @@ pub const MAX_FETCH_URL_BYTES: usize = 4096; const CONNECT_TIMEOUT: Duration = Duration::from_secs(10); const CONTROL_WAIT: Duration = Duration::from_secs(10); const CONTROL_POLL: Duration = Duration::from_millis(20); -const READ_CHUNK: usize = 64 * 1024; /// One requested package fetch. Every field is operator input, not a grant. #[derive(Clone, Debug)] @@ -340,82 +339,115 @@ fn perform( ) -> Result<(FetchReport, u16), FailedFetch> { let timeout = Duration::from_millis(start.timeout_ms); let deadline = Instant::now() + timeout; - let client = Client::builder() - .no_proxy() - .redirect(Policy::none()) - .retry(reqwest::retry::never()) - .connect_timeout(CONNECT_TIMEOUT.min(timeout)) - .timeout(timeout) + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_io() + .enable_time() .build() .map_err(|error| FailedFetch { - error: FetchError::Transport(error), + error: FetchError::Body(io::Error::other(format!( + "the fetch runtime could not start: {error}" + ))), reason: PackageFetchFailure::Transport, status: None, })?; - let mut response = client - .get(url.as_str()) - .send() - .map_err(|error| FailedFetch { - error: FetchError::Transport(error), - reason: PackageFetchFailure::Transport, - status: None, - })?; - let http_status = response.status().as_u16(); - if !response.status().is_success() { - return Err(FailedFetch { - error: FetchError::Status(http_status), - reason: PackageFetchFailure::Status, - status: Some(http_status), - }); - } - - let temporary = cache.join(format!(".{}.partial-{}", start.output_name, start.fetch_id)); - let mut file = open_cache_file(&temporary).map_err(|error| FailedFetch { - error: FetchError::Cache(error), - reason: PackageFetchFailure::Cache, - status: Some(http_status), - })?; - let mut hasher = Sha256::new(); - let mut bytes = 0u64; - let mut buffer = vec![0u8; READ_CHUNK]; - loop { - // reqwest's timeout bounds each read, not the whole response. A - // server that trickles bytes must not extend the fetch indefinitely. - if Instant::now() >= deadline { - drop(file); - let _ = fs::remove_file(&temporary); - return Err(FailedFetch { - error: FetchError::Body(io::Error::new( - io::ErrorKind::TimedOut, - "the package fetch deadline passed", - )), + runtime.block_on(async { + let client = Client::builder() + .no_proxy() + .redirect(Policy::none()) + .retry(reqwest::retry::never()) + .connect_timeout(CONNECT_TIMEOUT.min(timeout)) + .build() + .map_err(|error| FailedFetch { + error: FetchError::Transport(error), reason: PackageFetchFailure::Transport, + status: None, + })?; + let remaining = deadline.saturating_duration_since(Instant::now()); + let mut response = + match tokio::time::timeout(remaining, client.get(url.as_str()).send()).await { + Ok(Ok(response)) => response, + Ok(Err(error)) => { + return Err(FailedFetch { + error: FetchError::Transport(error), + reason: PackageFetchFailure::Transport, + status: None, + }); + } + Err(_) => return Err(deadline_failure(None)), + }; + let http_status = response.status().as_u16(); + if !response.status().is_success() { + return Err(FailedFetch { + error: FetchError::Status(http_status), + reason: PackageFetchFailure::Status, status: Some(http_status), }); } - let read = match response.read(&mut buffer) { - Ok(0) => break, - Ok(read) => read, - Err(error) => { + + let temporary = cache.join(format!(".{}.partial-{}", start.output_name, start.fetch_id)); + let mut file = open_cache_file(&temporary).map_err(|error| FailedFetch { + error: FetchError::Cache(error), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + })?; + let mut hasher = Sha256::new(); + let mut bytes = 0u64; + loop { + let remaining = deadline.saturating_duration_since(Instant::now()); + let chunk = match tokio::time::timeout(remaining, response.chunk()).await { + Ok(Ok(Some(chunk))) => chunk, + Ok(Ok(None)) => { + // A body that completes at or after the deadline is + // rejected, not published. + if Instant::now() >= deadline { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(deadline_failure(Some(http_status))); + } + break; + } + Ok(Err(error)) => { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(FailedFetch { + error: FetchError::Transport(error), + reason: PackageFetchFailure::Transport, + status: Some(http_status), + }); + } + Err(_) => { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(deadline_failure(Some(http_status))); + } + }; + if Instant::now() >= deadline { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(deadline_failure(Some(http_status))); + } + bytes = bytes.saturating_add(u64::try_from(chunk.len()).unwrap_or(u64::MAX)); + if bytes > start.max_bytes { + drop(file); let _ = fs::remove_file(&temporary); return Err(FailedFetch { - error: FetchError::Body(error), - reason: PackageFetchFailure::Transport, + error: FetchError::TooLarge(start.max_bytes), + reason: PackageFetchFailure::TooLarge, status: Some(http_status), }); } - }; - bytes = bytes.saturating_add(u64::try_from(read).unwrap_or(u64::MAX)); - if bytes > start.max_bytes { - drop(file); - let _ = fs::remove_file(&temporary); - return Err(FailedFetch { - error: FetchError::TooLarge(start.max_bytes), - reason: PackageFetchFailure::TooLarge, - status: Some(http_status), - }); + if let Err(error) = file.write_all(&chunk) { + drop(file); + let _ = fs::remove_file(&temporary); + return Err(FailedFetch { + error: FetchError::Cache(error), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + }); + } + hasher.update(&chunk); } - if let Err(error) = file.write_all(&buffer[..read]) { + if let Err(error) = file.sync_all() { drop(file); let _ = fs::remove_file(&temporary); return Err(FailedFetch { @@ -424,70 +456,71 @@ fn perform( status: Some(http_status), }); } - hasher.update(&buffer[..read]); - } - if let Err(error) = file.sync_all() { drop(file); - let _ = fs::remove_file(&temporary); - return Err(FailedFetch { - error: FetchError::Cache(error), - reason: PackageFetchFailure::Cache, - status: Some(http_status), - }); - } - drop(file); - // The cache is private to the user; a same-user writer can already change - // it. Refuse a name that appeared after the pre-check, and publish with a - // hard link so two concurrent fetches cannot replace each other's file. - // The link fails atomically when the destination exists. No power-loss - // claim is made for this cache. - match fs::symlink_metadata(final_path) { - Ok(_) => { - let _ = fs::remove_file(&temporary); - return Err(FailedFetch { - error: FetchError::AlreadyCached(start.output_name.clone()), - reason: PackageFetchFailure::Cache, - status: Some(http_status), - }); + // The cache is private to the user; a same-user writer can already + // change it. Refuse a name that appeared after the pre-check, and + // publish with a hard link so two concurrent fetches cannot replace + // each other's file. The link fails atomically when the destination + // exists. No power-loss claim is made for this cache. + match fs::symlink_metadata(final_path) { + Ok(_) => { + let _ = fs::remove_file(&temporary); + return Err(FailedFetch { + error: FetchError::AlreadyCached(start.output_name.clone()), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + }); + } + Err(error) if error.kind() == io::ErrorKind::NotFound => {} + Err(error) => { + let _ = fs::remove_file(&temporary); + return Err(FailedFetch { + error: FetchError::Cache(error), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + }); + } } - Err(error) if error.kind() == io::ErrorKind::NotFound => {} - Err(error) => { + if let Err(error) = publish_cache_file(&temporary, final_path, &start.output_name) { let _ = fs::remove_file(&temporary); return Err(FailedFetch { - error: FetchError::Cache(error), + error, reason: PackageFetchFailure::Cache, status: Some(http_status), }); } - } - if let Err(error) = publish_cache_file(&temporary, final_path, &start.output_name) { - let _ = fs::remove_file(&temporary); - return Err(FailedFetch { - error, - reason: PackageFetchFailure::Cache, - status: Some(http_status), - }); - } - Ok(( - FetchReport { - fetch_id: start.fetch_id, - profile_id: start.profile_id, - profile_name: start.profile_name.clone(), - profile_revision: start.profile_revision, - url_sha256: start.url_sha256.clone(), - output: NativePath::from_path(final_path).map_err(|error| FailedFetch { - error: FetchError::Cache(io::Error::other(error.to_string())), - reason: PackageFetchFailure::Cache, - status: Some(http_status), - })?, - bytes, - sha256: hex(&hasher.finalize()), - status: http_status, - }, - http_status, - )) + Ok(( + FetchReport { + fetch_id: start.fetch_id, + profile_id: start.profile_id, + profile_name: start.profile_name.clone(), + profile_revision: start.profile_revision, + url_sha256: start.url_sha256.clone(), + output: NativePath::from_path(final_path).map_err(|error| FailedFetch { + error: FetchError::Cache(io::Error::other(error.to_string())), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + })?, + bytes, + sha256: hex(&hasher.finalize()), + status: http_status, + }, + http_status, + )) + }) +} + +fn deadline_failure(status: Option) -> FailedFetch { + FailedFetch { + error: FetchError::Body(io::Error::new( + io::ErrorKind::TimedOut, + "the package fetch deadline passed", + )), + reason: PackageFetchFailure::Transport, + status, + } } fn open_cache_file(path: &Path) -> io::Result { diff --git a/crates/omgcode-core/src/packages/tests.rs b/crates/omgcode-core/src/packages/tests.rs index 1a9c2f2..de26818 100644 --- a/crates/omgcode-core/src/packages/tests.rs +++ b/crates/omgcode-core/src/packages/tests.rs @@ -1,4 +1,5 @@ use super::*; +use std::io::Read; use std::net::{SocketAddr, TcpListener, TcpStream}; use std::path::PathBuf; use std::thread::{self, JoinHandle}; @@ -496,7 +497,7 @@ fn a_trickled_body_cannot_outlive_the_fetch_deadline() { let started = Instant::now(); let error = fetch(&scene.data, &request).expect_err("the overall deadline must stop the fetch"); assert!( - started.elapsed() < Duration::from_secs(3), + started.elapsed() < Duration::from_secs(2), "the fetch ran past its deadline: {:?}", started.elapsed() ); diff --git a/crates/omgcode-core/src/run.rs b/crates/omgcode-core/src/run.rs index 230a014..7720d9d 100644 --- a/crates/omgcode-core/src/run.rs +++ b/crates/omgcode-core/src/run.rs @@ -360,13 +360,14 @@ pub fn run( "trusted runs exist only on Linux and Windows", ))?; let control_path = ControlStore::journal_path(data_root); - let (journal_root, store_id, mut prepared, owner, baseline, revocations) = { + let (journal_root, store_id, mut prepared, owner, baseline, revocations, model_revocations) = { let control = open_control(data_root)?; let (journal_root, store_id) = control.file_audit_context()?; let prepared = prepare(&control, request, platform, &journal_root)?; // Taken under the control lock, so every later change is seen. let baseline = stamp(&control_path)?; let revocations = control.grant_revocations(request.workspace_id); + let model_revocations = control.model_tool_revocations(request.workspace_id); let owner = OwnerLock::acquire(&journal_root, prepared.start.run_id)?; let mut journal = RunJournal::open(&journal_root, store_id)?; journal.start(prepared.start.clone())?; @@ -377,6 +378,7 @@ pub fn run( owner, baseline, revocations, + model_revocations, ) }; let run_id = prepared.start.run_id; @@ -389,6 +391,7 @@ pub fn run( control_path: &control_path, start: &prepared.start, revocations, + model_revocations, caller: cancel, stop: &stop, done: &done, @@ -458,6 +461,8 @@ struct Watch<'a> { start: &'a RunStart, /// Container revocations of the workspace recorded when the run started. revocations: u64, + /// `model_tools` true-to-false changes recorded when the run started. + model_revocations: u64, caller: &'a CancelSignal, stop: &'a CancelSignal, done: &'a AtomicBool, @@ -491,12 +496,21 @@ impl Watch<'_> { match ControlStore::open_existing(self.data_root) { Ok(Some(control)) => { *seen = current; - withdrawn(&control, self.start).or_else(|| { - // A grant can be added and revoked between two polls, so - // its state alone can miss a revocation; the count cannot. - (control.grant_revocations(self.start.workspace_id) != self.revocations) - .then_some("a container grant of this workspace was revoked") - }) + withdrawn(&control, self.start) + .or_else(|| { + // A disable followed by a re-enable can happen between + // polls, so the current value alone is not enough. The + // durable count records the withdrawal. + (control.model_tool_revocations(self.start.workspace_id) + != self.model_revocations) + .then_some("the workspace model_tools preference was revoked") + }) + .or_else(|| { + // A grant can be added and revoked between two polls, so + // its state alone can miss a revocation; the count cannot. + (control.grant_revocations(self.start.workspace_id) != self.revocations) + .then_some("a container grant of this workspace was revoked") + }) } Ok(None) => Some("the settings were removed"), Err(ControlError::Busy) => None, @@ -1716,6 +1730,7 @@ mod tests { Some("the workspace model_tools preference is disabled") ); assert_eq!(withdrawn(&control, &plain), None); + let baseline = control.model_tool_revocations(workspace_id); drop(control); let mut control = ControlStore::open(&data).expect("control store"); control @@ -1729,9 +1744,34 @@ mod tests { control.revision(), ) .expect("permissions"); + control + .set_permissions( + workspace_id, + PermissionSet { + execute: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("disable model tools"); + assert!(control.model_tool_revocations(workspace_id) > baseline); + control + .set_permissions( + workspace_id, + PermissionSet { + execute: true, + model_tools: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("re-enable model tools"); drop(control); let control = open_control(&data).expect("control"); + // The current value allows the run again, but the durable count still + // records the withdrawal, so a live watcher stops it. assert_eq!(withdrawn(&control, &model), None); + assert!(control.model_tool_revocations(workspace_id) > baseline); } /// A configured cache-root change withdraws a run that holds the old diff --git a/crates/omgcode-platform/src/process/disk.rs b/crates/omgcode-platform/src/process/disk.rs index 9167ad7..7bc8884 100644 --- a/crates/omgcode-platform/src/process/disk.rs +++ b/crates/omgcode-platform/src/process/disk.rs @@ -262,23 +262,27 @@ fn sample_until_stopped( return; } } - Err(_) if state.stop.load(Ordering::SeqCst) => { - // Cancellation interrupted this scan. That is not a - // measurement failure; the supervisor is already stopping. - return; - } Err(error) => { - // Only the first error matters; the supervisor stops the tree. - let mut slot = state.error.lock().unwrap_or_else(PoisonError::into_inner); - if slot.is_none() { - *slot = Some(error); - } + record_sample_error(state, error); return; } } } } +/// Record a scan error unless it is the cancellation that shutdown itself +/// requested. A real error such as a permission denial is preserved even if +/// `stop` was set at the same time, so `finish` fails closed. +fn record_sample_error(state: &DiskState, error: io::Error) { + if error.kind() == io::ErrorKind::Interrupted && state.stop.load(Ordering::SeqCst) { + return; + } + let mut slot = state.error.lock().unwrap_or_else(PoisonError::into_inner); + if slot.is_none() { + *slot = Some(error); + } +} + fn take_error(state: &DiskState) -> Option { // The lock guards only this `Option`; a poisoned lock leaves it readable. state @@ -623,6 +627,20 @@ mod tests { assert!(measured.is_err(), "an unreadable directory was measured"); } + #[test] + fn cancellation_does_not_hide_a_real_measurement_error() { + let state = DiskState::default(); + state.stop.store(true, Ordering::SeqCst); + record_sample_error(&state, io::Error::from(io::ErrorKind::PermissionDenied)); + let error = take_error(&state).expect("a real error survives shutdown"); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + + let state = DiskState::default(); + state.stop.store(true, Ordering::SeqCst); + record_sample_error(&state, io::Error::from(io::ErrorKind::Interrupted)); + assert!(take_error(&state).is_none()); + } + #[test] fn a_cancelled_measurement_reports_interruption() { let directory = tempfile::tempdir().expect("temporary directory"); diff --git a/docs/execution.md b/docs/execution.md index 0c2e268..156bfa0 100644 --- a/docs/execution.md +++ b/docs/execution.md @@ -272,8 +272,10 @@ omgcode workspace clear-package-cache --revision must not overlap application storage or the development root, and must not overlap another workspace's root. Clearing or replacing a root revokes read-execute container grants that covered the old root, and selecting a -directory another workspace's container can already read is refused. Runs that -held the old exposure are stopped before the command completes. +directory another workspace's container can already read is refused. A stale +foreign grant record refuses the selection because a renamed directory keeps +its ACL and separation cannot be proved. Runs that held the old exposure are +stopped before the command completes. `clear-package-cache` removes the selection and leaves cached files in place. Without a selected root, Linux uses the application-storage fallback and Windows has none. A 0.23.0 control journal @@ -295,8 +297,8 @@ variables are ignored, no credentials are sent, and nothing is retried. TLS uses the operating-system trust store through `reqwest`'s `native-tls` feature; certificate validation is never disabled. The response must be 2xx and its body is streamed, hashed with SHA-256, and stopped if it passes the -byte limit. The overall deadline is checked before each read, so a server that -trickles bytes cannot extend the fetch past its requested timeout. +byte limit. The request send and every body read are bounded by the remaining +deadline, and a body that completes after it is rejected. The file is written as a private `.partial-` entry, synced, and published with an atomic no-replace hard link into the selected workspace @@ -656,7 +658,10 @@ cgroup. Version 0.25.0 adds `workspace_run_command` to the core turn runtime. The runtime advertises it only when the session workspace's live `model_tools`, `execute`, `read`, and `write` preferences are enabled. Each call rechecks -them, and the trusted run checks them again. +them, and the trusted run checks them again. A model-origin run records a +durable per-workspace count of `model_tools` true-to-false changes; disabling +the preference stops the run even if it is re-enabled before the watcher reads +the setting. Arguments are a recorded toolchain `profile`, a `program`, at most 64 `arguments`, and an optional workspace-relative `cwd`. The tool has no shell diff --git a/docs/testing-standards.md b/docs/testing-standards.md index a8705fb..d9f1ab5 100644 --- a/docs/testing-standards.md +++ b/docs/testing-standards.md @@ -143,7 +143,7 @@ As the relevant components are implemented, include: | Processes | Descendant escape attempts, limits, cleanup, startup failure | | Disk limits | Sampled growth ends the whole run as `disk`, baseline excludes prior content, links and reparse points are not followed, the ceiling stops a scan early, unreadable or oversized roots fail closed | | Networking | Disabled egress, host-local access, redirects, provider routing | -| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, an overall deadline under a trickled body, atomic no-replace publication under a concurrent collision, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, grant revocation when a root changes or clears, refusal of a root another workspace can already read, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one or without a selected root. No run accepts a profile. A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | +| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, remaining-time send and body reads under a trickled response, atomic no-replace publication under a concurrent collision, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, grant revocation when a root changes or clears, stale foreign-grant refusal, refusal of a root another workspace can already read, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one or without a selected root. No run accepts a profile. A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | | Providers | Partial/malformed streams, duplicate calls, interruption, retries | | Provider profiles | Kind/authentication pairing, duplicate and invalid names, case-insensitive lookup, revisioned add/remove, replay, no credential or endpoint storage, and capability reporting. No adapter or login is exercised | | Approvals | Request validation, pending/list/show, approve/deny, single-use consume, expiry, binding mismatch, settings-revision invalidation, replay, unknown IDs, and limits. No operation executes | @@ -154,12 +154,12 @@ As the relevant components are implemented, include: | Frontend | Untrusted rendering, IPC authorization, no secret disclosure | | Sessions | Concurrent edits/worktrees, JSONL naming/schema/durability, index rebuild, archive/restore, safe resume, v1/v2 boundaries | | Turn/tool lifecycle | Fresh bindings, strict schemas, unknown/malformed/duplicate calls, round/call/byte limits, cancellation, deadline, source labels, denial at the broker, no replay | -| Process-tree runner | Exit codes, exact environment, argument quoting, bounded output, deadline, cancellation, descendant escape attempts, inherited handles, observed cleanup, cancellable disk sampling, bounded traversal queues, invalid specifications | +| Process-tree runner | Exit codes, exact environment, argument quoting, bounded output, deadline, cancellation, descendant escape attempts, inherited handles, observed cleanup, cancellable disk sampling, bounded traversal queues, real scan errors preserved during shutdown, invalid specifications | | Linux confinement | Read-only and read-write rules, path truncation, denied host files and directories, inheritance by descendants, IP and `io_uring` denial in both Unix-socket modes, Unix-socket and datagram-pair denial in `PairsOnly`, host Unix-socket connection, workspace socket binding, and datagram pairs in `HostServices`, unexecutable programs, missing/link/special confinement paths, ABI selection below and above the minimum | | Windows confinement | Granted read-only and read-write paths, denied host files, directories, named pipe, and loopback TCP, a writable container temporary directory, inheritance by descendants, the same probes unconfined, missing, weaker, and foreign grants, a missing profile, `LOCALAPPDATA`, or container name, refused `HostServices`, an ungranted program | | Container grants | SID derivation, propagation to existing and new children, exact read-back, protection state kept, junctions not followed, hard-link reach, refused replaced/junction/file/relative paths, profile creation and deletion, journal intent and outcome order, replay validation, revision rules, scope refusals, pending and revoking recovery, the revocation reserve, CLI grant/list/revoke, refusal off Windows | | Model tool chat | Preference off by default and rechecked per call, settings compatibility, confinement against parent, absolute, rooted, UNC, link, junction, and hard-link paths, strict provider tool-call parsing, token removal before journal and model, refusal of token-bearing calls, withheld replay after revocation, finish reason, one call per turn in escape cases | -| Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, no replay, and revoking `model_tools` stops a live model-origin run. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | +| Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, no replay, and a durable `model_tools` revocation count that stops a model-origin run even after re-enable. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | | MCP | Version/transport compatibility, authorization, untrusted content, local confinement, OAuth/redirects, cancellation | | LM Studio | Model discovery, token/no-token profiles, streamed tools, context limits, unavailable models, no hosted-tool bypass | | Instructions | Compiled base/UI/request match, runtime replacement denial, role-injection attempts, preference/task precedence, revisions, safe rendering | From 97c733e8493252078e0a1bbae485c6ba52e64d29 Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sat, 14 Nov 2026 23:38:54 +0000 Subject: [PATCH 3/3] fix: run package fetch file I/O asynchronously Sonar flagged blocking file calls inside the async deadline path. Open, write, sync, metadata, removal, and publication now use Tokio's filesystem APIs so the remaining-time deadline never blocks a runtime thread on disk I/O. The concurrent publication test drives the async helper on its own runtime. Co-Authored-By: Claude Code --- Cargo.toml | 2 +- crates/omgcode-core/src/packages.rs | 69 +++++++++++++---------- crates/omgcode-core/src/packages/tests.rs | 7 ++- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4339062..5eefd2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ serde_json = "1" uuid = { version = "1", features = ["v4", "serde"] } time = { version = "0.3", features = ["formatting", "parsing", "serde"] } thiserror = "2" -tokio = { version = "1", default-features = false, features = ["rt", "time", "net"] } +tokio = { version = "1", default-features = false, features = ["rt", "time", "net", "fs", "io-util"] } reqwest = { version = "0.13.5", default-features = false, features = ["blocking", "json", "native-tls"] } sha2 = "0.11.1" clap = { version = "4.6.7", features = ["derive"] } diff --git a/crates/omgcode-core/src/packages.rs b/crates/omgcode-core/src/packages.rs index abac965..2124115 100644 --- a/crates/omgcode-core/src/packages.rs +++ b/crates/omgcode-core/src/packages.rs @@ -12,8 +12,8 @@ //! record; no automatic retry follows. See `docs/execution.md` and ADR-0035. use std::fmt::Write as _; -use std::fs::{self, OpenOptions}; -use std::io::{self, Write}; +use std::fs; +use std::io; use std::path::Path; use std::thread; use std::time::{Duration, Instant}; @@ -24,6 +24,7 @@ use reqwest::redirect::Policy; use serde::Serialize; use sha2::{Digest, Sha256}; use thiserror::Error; +use tokio::io::AsyncWriteExt; use uuid::Uuid; use crate::control::{ @@ -319,15 +320,20 @@ fn matching_origin(profile: &PackageProfile, url: &Url) -> Result<(), FetchError /// replacing an existing entry. A hard link is one atomic no-replace /// operation in the same directory; a concurrent winner leaves this caller /// with `AlreadyCached`. The cache is not power-loss qualified. -fn publish_cache_file(temporary: &Path, final_path: &Path, name: &str) -> Result<(), FetchError> { - fs::hard_link(temporary, final_path).map_err(|error| { - if fs::symlink_metadata(final_path).is_ok() { +async fn publish_cache_file( + temporary: &Path, + final_path: &Path, + name: &str, +) -> Result<(), FetchError> { + if let Err(error) = tokio::fs::hard_link(temporary, final_path).await { + let already = tokio::fs::symlink_metadata(final_path).await.is_ok(); + return Err(if already { FetchError::AlreadyCached(name.to_owned()) } else { FetchError::Cache(error) - } - })?; - let _ = fs::remove_file(temporary); + }); + } + let _ = tokio::fs::remove_file(temporary).await; Ok(()) } @@ -385,11 +391,13 @@ fn perform( } let temporary = cache.join(format!(".{}.partial-{}", start.output_name, start.fetch_id)); - let mut file = open_cache_file(&temporary).map_err(|error| FailedFetch { - error: FetchError::Cache(error), - reason: PackageFetchFailure::Cache, - status: Some(http_status), - })?; + let mut file = open_cache_file(&temporary) + .await + .map_err(|error| FailedFetch { + error: FetchError::Cache(error), + reason: PackageFetchFailure::Cache, + status: Some(http_status), + })?; let mut hasher = Sha256::new(); let mut bytes = 0u64; loop { @@ -401,14 +409,14 @@ fn perform( // rejected, not published. if Instant::now() >= deadline { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(deadline_failure(Some(http_status))); } break; } Ok(Err(error)) => { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::Transport(error), reason: PackageFetchFailure::Transport, @@ -417,28 +425,28 @@ fn perform( } Err(_) => { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(deadline_failure(Some(http_status))); } }; if Instant::now() >= deadline { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(deadline_failure(Some(http_status))); } bytes = bytes.saturating_add(u64::try_from(chunk.len()).unwrap_or(u64::MAX)); if bytes > start.max_bytes { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::TooLarge(start.max_bytes), reason: PackageFetchFailure::TooLarge, status: Some(http_status), }); } - if let Err(error) = file.write_all(&chunk) { + if let Err(error) = file.write_all(&chunk).await { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::Cache(error), reason: PackageFetchFailure::Cache, @@ -447,9 +455,9 @@ fn perform( } hasher.update(&chunk); } - if let Err(error) = file.sync_all() { + if let Err(error) = file.sync_all().await { drop(file); - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::Cache(error), reason: PackageFetchFailure::Cache, @@ -463,9 +471,9 @@ fn perform( // publish with a hard link so two concurrent fetches cannot replace // each other's file. The link fails atomically when the destination // exists. No power-loss claim is made for this cache. - match fs::symlink_metadata(final_path) { + match tokio::fs::symlink_metadata(final_path).await { Ok(_) => { - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::AlreadyCached(start.output_name.clone()), reason: PackageFetchFailure::Cache, @@ -474,7 +482,7 @@ fn perform( } Err(error) if error.kind() == io::ErrorKind::NotFound => {} Err(error) => { - let _ = fs::remove_file(&temporary); + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error: FetchError::Cache(error), reason: PackageFetchFailure::Cache, @@ -482,8 +490,8 @@ fn perform( }); } } - if let Err(error) = publish_cache_file(&temporary, final_path, &start.output_name) { - let _ = fs::remove_file(&temporary); + if let Err(error) = publish_cache_file(&temporary, final_path, &start.output_name).await { + let _ = tokio::fs::remove_file(&temporary).await; return Err(FailedFetch { error, reason: PackageFetchFailure::Cache, @@ -523,15 +531,14 @@ fn deadline_failure(status: Option) -> FailedFetch { } } -fn open_cache_file(path: &Path) -> io::Result { - let mut options = OpenOptions::new(); +async fn open_cache_file(path: &Path) -> io::Result { + let mut options = tokio::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] { - use std::os::unix::fs::OpenOptionsExt; options.mode(0o600); } - options.open(path) + options.open(path).await } /// Open existing settings, waiting briefly for another command's lock. diff --git a/crates/omgcode-core/src/packages/tests.rs b/crates/omgcode-core/src/packages/tests.rs index de26818..fc6540d 100644 --- a/crates/omgcode-core/src/packages/tests.rs +++ b/crates/omgcode-core/src/packages/tests.rs @@ -1,5 +1,5 @@ use super::*; -use std::io::Read; +use std::io::{Read, Write}; use std::net::{SocketAddr, TcpListener, TcpStream}; use std::path::PathBuf; use std::thread::{self, JoinHandle}; @@ -404,7 +404,10 @@ fn concurrent_publication_never_replaces_the_winner() { let final_path = final_path.clone(); scope.spawn(move || { barrier.wait(); - publish_cache_file(temporary, &final_path, "pkg.bin") + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .expect("publication runtime"); + runtime.block_on(publish_cache_file(temporary, &final_path, "pkg.bin")) }) }) .collect();