Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ pub fn count_tokens(text: &str) -> usize {
text.split_whitespace().count()
}

/// Returns true when rtk is executing as a Claude Code (or other AI IDE) hook.
/// Set by each hook entry point before any other work so that diagnostic
/// eprintln! calls in filter/trust paths are suppressed — any stderr output
/// during hook execution triggers Claude Code bug #4669 and permanently
/// disables the hook for the session.
pub fn in_hook_mode() -> bool {
std::env::var("RTK_HOOK_MODE").is_ok()
}

/// Detect the package manager used in the current directory.
/// Returns "pnpm", "yarn", or "npm" based on lockfile presence.
///
Expand Down Expand Up @@ -839,6 +848,22 @@ mod tests {
assert_eq!(human_bytes(1_099_511_627_776), "1.0 TB");
}

#[test]
fn test_in_hook_mode_unset() {
std::env::remove_var("RTK_HOOK_MODE");
assert!(!in_hook_mode(), "in_hook_mode() must return false when RTK_HOOK_MODE is unset");
}

#[test]
fn test_in_hook_mode_set() {
#[allow(deprecated)]
std::env::set_var("RTK_HOOK_MODE", "1");
let result = in_hook_mode();
#[allow(deprecated)]
std::env::remove_var("RTK_HOOK_MODE");
assert!(result, "in_hook_mode() must return true when RTK_HOOK_MODE=1");
}

#[test]
fn test_count_tokens_basic() {
assert_eq!(count_tokens("hello world"), 2);
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ fn load_permission_rules() -> (Vec<String>, Vec<String>, Vec<String>) {
continue;
};
let Ok(json) = serde_json::from_str::<Value>(&content) else {
eprintln!(
"[rtk] warning: failed to parse permissions from {}",
path.display()
);
if !crate::core::utils::in_hook_mode() {
eprintln!(
"[rtk] warning: failed to parse permissions from {}",
path.display()
);
}
continue;
};
let Some(permissions) = json.get("permissions") else {
Expand Down