Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(host-core): sample the Windows process snapshot clock

Linuxdazhao3bcc90e5afc3
diff --git a/crates/host-core/Cargo.toml b/crates/host-core/Cargo.toml
index 6e6adb7e6..a1a6c1ff6 100644
--- a/crates/host-core/Cargo.toml
+++ b/crates/host-core/Cargo.toml
@@ -45,6 +45,7 @@ windows-sys = { version = "0.61.2", features = [
"Win32_System_JobObjects",
"Win32_System_LibraryLoader",
"Win32_System_Registry",
+ "Win32_System_SystemInformation",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
] }
diff --git a/crates/host-core/src/tools/windows_process_tree.rs b/crates/host-core/src/tools/windows_process_tree.rs
index 89ca3da54..5c7b389cc 100644
--- a/crates/host-core/src/tools/windows_process_tree.rs
+++ b/crates/host-core/src/tools/windows_process_tree.rs
@@ -30,11 +30,11 @@ pub(super) fn terminate_tree(root_pid: u32) -> Result<(), String> {
if root_pid == 0 {
return Ok(());
}
- let snapshot = Snapshot::capture()?;
// A process created after the snapshot cannot be one the snapshot
// described, so its creation time is the watermark that makes a recycled
// PID distinguishable from the real descendant.
- let watermark = ProcessTimes::now()?;
+ let watermark = ProcessTimes::now();
+ let snapshot = Snapshot::capture()?;
let mut errors: Vec<String> = Vec::new();
for pid in snapshot.descendants_deepest_first(root_pid) {
@@ -223,11 +223,18 @@ impl ProcessTimes {
})
}
- /// Creation time of the current process, used as the snapshot watermark.
- fn now() -> Result<Self, String> {
- let current = unsafe { windows_sys::Win32::System::Threading::GetCurrentProcess() };
- Self::of(current)
- .map_err(|error| format!("GetProcessTimes failed for the current process: {error}"))
+ /// Current wall clock in the same FILETIME units as process creation times.
+ fn now() -> Self {
+ let mut now = FILETIME {
+ dwLowDateTime: 0,
+ dwHighDateTime: 0,
+ };
+ unsafe {
+ windows_sys::Win32::System::SystemInformation::GetSystemTimeAsFileTime(&mut now);
+ }
+ Self {
+ created: (u64::from(now.dwHighDateTime) << 32) | u64::from(now.dwLowDateTime),
+ }
}
fn created_after(&self, watermark: &Self) -> bool {
@@ -246,12 +253,7 @@ mod tests {
#[test]
fn children_are_terminated_before_their_parents() {
- let edges = HashMap::from([
- (10, vec![11]),
- (11, vec![12]),
- (12, vec![13]),
- (11, vec![14]),
- ]);
+ let edges = HashMap::from([(10, vec![11]), (11, vec![12, 14]), (12, vec![13])]);
let order = Snapshot { edges }.descendants_deepest_first(10);
let position = |pid: u32| order.iter().position(|entry| *entry == pid).unwrap();
assert!(position(13) < position(12));
@@ -290,4 +292,30 @@ mod tests {
assert!(!ProcessTimes::from_raw(1_000).created_after(&watermark));
assert!(!ProcessTimes::from_raw(999).created_after(&watermark));
}
+
+ #[cfg(windows)]
+ #[test]
+ fn snapshot_watermark_includes_processes_started_after_the_host() {
+ use std::os::windows::{io::AsRawHandle, process::CommandExt};
+
+ let mut child = std::process::Command::new("powershell.exe")
+ .args([
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ "Start-Sleep -Seconds 120",
+ ])
+ .creation_flags(windows_sys::Win32::System::Threading::CREATE_NO_WINDOW)
+ .spawn()
+ .unwrap();
+ let created = ProcessTimes::of(child.as_raw_handle() as HANDLE);
+ let watermark = ProcessTimes::now();
+ child.kill().unwrap();
+ child.wait().unwrap();
+
+ assert!(
+ !created.unwrap().created_after(&watermark),
+ "a process spawned after the Host must remain eligible for cleanup"
+ );
+ }
}
diff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.md
index 5b1d1c161..b0297088b 100644
--- a/docs/spec/03-runtime/03-tools-and-permissions.md
+++ b/docs/spec/03-runtime/03-tools-and-permissions.md
@@ -536,6 +536,11 @@ Host execution baseline:
process survives `tools.abort` and `agent.abort`. The host owns the process
tree, reports it through the composer's shared container, and stops it on
request or on app shutdown. Each stream keeps its last 256KB.
+- On Windows, descendant cleanup samples the current FILETIME before the
+ process snapshot. A process created after Host startup but before that
+ snapshot remains eligible for cleanup; Host creation time is not a snapshot
+ watermark. Recycled PID, handle identity, child-first ordering and observable
+ cleanup-error safeguards remain.
- **Background tasks belong to the session that started them.** The session id is
fixed at spawn and never changes: switching, renaming, archiving, or restoring
a session does not re-home its tasks. A session runs at most **10** concurrent
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 55ded6cb5..f3e3e63b8 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -25,6 +25,19 @@
deterministic regression does not claim to recover that missing evidence.
+### Windows background process snapshot clock
+
+- **Preconditions:** Isolated Windows Host and a synthetic background task.
+- **Steps:** Start a child and grandchild after Host startup, exercise stopping
+ through the production process-tree helper, and observe both owned processes.
+- **Expected:** Children created before the snapshot are not rejected as recycled
+ PIDs merely because they started after the Host. Descendants terminate before
+ their parent; unrelated/newer processes remain excluded and failures surface.
+- **Coverage:** `windows_process_tree` unit tests, including a real owned child
+ creation-time check; `e2e-background-tasks-host.mjs` for the Host workflow.
+- **Qualification:** A foreign-API fixture and Windows target compilation are
+ separate from native Windows execution; record which actually ran.
+
## 1. Goals
### E2E-TRANSCRIPT-scroll-interruption