grok-build-upstream-mirror/crates/codegen/xai-grok-shell/src/util/mod.rs
grokkybara[bot] a422116582 Synced from monorepo
Synced from monorepo

Changes:
- Release a shell session's resources in one drop
- Make the tools blocking-wait cap client-configurable and self-describing
- Recognize API "exceeds budget" errors as context overflow
- Retry /btw on model overload
- Carry running background tasks and subagents across compaction
- Require round-trip time for SDK liveness checks
- Background-subagent completion reminders with a selectable delivery surface
- Make a PTY shell reap itself until it reaches the registry
- Recover the OS error code from a TLS-phase connection reset
- Consume the attached-client signal and report why idle is withheld
- Treat `.grok/sandbox.toml` edits as protected so auto mode prompts before writing
- Surface history/search in the Ctrl+. cheatsheet and keep it working in history view
- Delete sessions from the dashboard and welcome list
- Release a session's activity record when the session ends
- Stop charging auth-retry budget for fail-closed 401s; reset it across suspends
- Scope skills watches on project vendor roots
- Make [stop] cancel in-flight compaction
- Make the leader soak measure the leader, not its harness

Source-Revision: 8d69c91f02bcacf01e98d5aebbf2f92547c45738
2026-07-31 18:08:03 +00:00

143 lines
4.2 KiB
Rust

pub mod config;
pub(crate) mod dual_clock;
pub mod grok_auth_credentials;
pub mod hooks;
pub mod limits;
pub(crate) mod subprocess;
pub(crate) mod user_identity;
// The foundation utilities live in `xai-grok-shell-base` (upstream of this
// crate so they build in parallel). Re-exported at the original paths so
// existing `crate::util::…` / `xai_grok_shell::util::…` users compile
// unchanged.
pub use xai_grok_shell_base::util::*;
pub(crate) fn is_user_instruction_path(
path: &std::path::Path,
grok_home: &std::path::Path,
vendor_homes: &[(std::path::PathBuf, bool)],
workspace_root: Option<&std::path::Path>,
) -> bool {
let parent = path.parent();
let grok_rules = grok_home.join("rules");
let is_exact_home_surface = parent
.is_some_and(|parent| parent == grok_home || parent == grok_rules)
|| vendor_homes.iter().any(|(vendor_home, named_enabled)| {
parent.is_some_and(|parent| {
(*named_enabled && parent == vendor_home) || parent == vendor_home.join("rules")
})
});
if is_exact_home_surface {
return true;
}
if workspace_root.is_some_and(|root| path.starts_with(root)) {
return false;
}
path.starts_with(grok_home)
|| vendor_homes
.iter()
.any(|(vendor_home, _)| path.starts_with(vendor_home))
}
/// Aborts the wrapped tokio task when dropped.
///
/// Use to tie a spawned helper task's lifetime to an async scope so that
/// cancelling the parent future (e.g. a turn abort dropping the tool loop)
/// also tears down the helper instead of leaving it running detached.
/// Aborting an already-finished task is a no-op, so this is safe to hold
/// across normal scope exit too.
pub struct AbortOnDrop(pub tokio::task::JoinHandle<()>);
impl Drop for AbortOnDrop {
fn drop(&mut self) {
self.0.abort();
}
}
/// Expand a leading `~` to the home directory; other paths pass through.
pub(crate) fn expand_home(s: &str) -> std::path::PathBuf {
if let Some(stripped) = s.strip_prefix("~/") {
if let Some(home) = dirs::home_dir() {
return home.join(stripped);
}
} else if s == "~"
&& let Some(home) = dirs::home_dir()
{
return home;
}
std::path::PathBuf::from(s)
}
#[cfg(test)]
mod expand_home_tests {
use super::expand_home;
#[test]
fn passthrough_for_absolute_path() {
assert_eq!(
expand_home("/abs/path"),
std::path::PathBuf::from("/abs/path")
);
}
#[test]
fn passthrough_for_relative_path() {
assert_eq!(
expand_home("rel/path"),
std::path::PathBuf::from("rel/path")
);
}
#[test]
fn bare_tilde() {
let home = dirs::home_dir().expect("home_dir required for this test");
assert_eq!(expand_home("~"), home);
}
#[test]
fn tilde_slash() {
let home = dirs::home_dir().expect("home_dir required for this test");
assert_eq!(expand_home("~/foo/bar"), home.join("foo/bar"));
}
#[test]
fn does_not_handle_user_tilde() {
// `~bob/path` is treated as a literal relative path.
assert_eq!(
expand_home("~bob/path"),
std::path::PathBuf::from("~bob/path")
);
}
}
#[cfg(test)]
mod is_user_instruction_path_tests {
use super::is_user_instruction_path;
use std::path::Path;
#[test]
fn grok_home_named_file_nested_in_workspace_is_user_scoped() {
assert!(is_user_instruction_path(
Path::new("/repo/config/AGENTS.md"),
Path::new("/repo/config"),
&[],
Some(Path::new("/repo")),
));
assert!(!is_user_instruction_path(
Path::new("/repo/config/src/AGENTS.md"),
Path::new("/repo/config"),
&[],
Some(Path::new("/repo")),
));
}
#[test]
fn workspace_descendants_under_grok_home_stay_project_scoped() {
assert!(!is_user_instruction_path(
Path::new("/custom/grok/worktrees/repo/src/AGENTS.md"),
Path::new("/custom/grok"),
&[],
Some(Path::new("/custom/grok/worktrees/repo")),
));
}
}