Files
Kigi-CLI/crates/codegen/kigi-hooks/src/trust.rs
T
ZacharyZhang-NY 6f31415ed6 §9 acceptance: grep-zero sweep — every internal x.ai/grok identifier renamed
The PRD's first acceptance gate now holds: grep -RinE '\bx\.ai\b|grok'
crates/ --include='*.rs' → 0 matches (exempt: NOTICE and third-party
license archives, README provenance, and the required 'Based on Grok
Build Open Source' attribution, now sourced from version_attribution.txt).

Wire-visible renames (both sides in this repo, changed in lockstep):
- Auth method id 'grok.com' → 'kimi-code' (AuthMethodKind::KimiCode).
- Every x.ai/* and _x.ai/* ACP ext method and meta key → kigi/* /
  _kigi/* (~200 names; grokShell → kigiShell). Session-file replay keeps
  a read-side alias for the legacy '_x.ai/session/update' method so
  existing updates.jsonl histories load; writes emit only the new name
  (both directions test-pinned).
- Agent types grok-build* → kigi* with a documented legacy-prefix alias
  at resolution time so persisted sessions keep resolving.
- ToolNamespace/BuiltinAgentName GrokBuild* → Kigi* (wire snake_case
  kigi/kigi_concise/kigi_hashline; schema regenerated); grok_build
  implementation dirs renamed to kigi*.
- x-grok-* headers → x-kigi-*, __GROK_* sentinels → __KIGI_*, themes
  grokday/groknight → kigiday/kiginight (old persisted values fall back
  to the default theme), web_fetch allowlist xAI hosts → kimi.com +
  moonshot platforms, changelog CDN → this repo, grok-build changelog
  archives deleted.
- BYOK default endpoint removed: [endpoints] api_base_url is now truly
  optional with NO default — consumers fail fast with the flag name when
  unset (no silent x.ai egress). Mock harnesses inject it explicitly.
- System-prompt identity fixed: 'released by xAI' → 'an unofficial
  community CLI for Kimi' (template + regenerated encrypted form).

Also repaired pre-existing grok-era test debt found by the sweep: the
stale trace_classify default-model pin, the grok-pager UA label test,
pty-harness stale-binary reuse and non-hermetic moonshot routing (a PTY
test could previously reach the real api.moonshot.cn), and the outdated
oauth fixture scope key.

Gates: §9 grep 0; fmt clean; workspace check/clippy 0/0 (-D warnings);
FULL cargo test --workspace: 234 suites, 21,961 passed, 0 failed;
deny advisories ok.
2026-07-18 02:48:46 -04:00

172 lines
6.2 KiB
Rust

use std::path::{Path, PathBuf};
// Project-hook trust is no longer stored here: the shell's folder-trust store
// (`~/.kigi/trusted_folders.toml`) is the single authority for whether a repo's
// project hooks run (the same gate as repo-local MCP/LSP). The helpers below
// exist only to migrate prior grants out of the legacy file.
/// Path to the legacy project-hook trust file
/// (`<user_kigi_home>/trusted-hook-projects`), or `None` when no user kigi home
/// resolves. Retained only for the one-time migration into folder-trust.
pub fn legacy_trust_file_path() -> Option<PathBuf> {
Some(kigi_config::user_kigi_home()?.join("trusted-hook-projects"))
}
/// Parse the legacy trusted-projects file into a list of project paths.
///
/// The legacy format is one canonical absolute path per line; blank and
/// `#`-comment lines are skipped. A missing file yields `Ok(empty)` (nothing to
/// migrate); any OTHER read error is returned as `Err` so the caller does not
/// mistake an unreadable file for an empty one and consume it. Consumed by the
/// one-time migration that seeds folder-trust from prior grants.
pub fn list_trusted_projects_with_file(trust_file: &Path) -> std::io::Result<Vec<PathBuf>> {
let content = match std::fs::read_to_string(trust_file) {
Ok(c) => c,
// A missing file is "nothing to migrate", not an error.
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(e),
};
Ok(content
.lines()
.map(str::trim)
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.map(PathBuf::from)
.collect())
}
// ── Hook enable/disable ─────────────────────────────────────────────────
/// Check whether a hook is disabled by name.
///
/// Disabled hooks are listed in , one hook name per line.
pub fn is_hook_disabled(hook_name: &str) -> bool {
match disabled_hooks_file_path() {
Some(file) => is_hook_disabled_with_file(hook_name, &file),
None => false,
}
}
fn is_hook_disabled_with_file(hook_name: &str, file: &Path) -> bool {
let content = match std::fs::read_to_string(file) {
Ok(c) => c,
Err(_) => return false,
};
content
.lines()
.any(|l| !l.trim().is_empty() && !l.trim().starts_with('#') && l.trim() == hook_name)
}
/// Disable a hook by name. Adds to .
pub fn disable_hook(hook_name: &str) -> Result<(), String> {
let file = disabled_hooks_file_path()
.ok_or_else(|| "no user kigi home (set $KIGI_SHARE_DIR or $HOME)".to_string())?;
disable_hook_with_file(hook_name, &file)
}
fn disable_hook_with_file(hook_name: &str, file: &Path) -> Result<(), String> {
if is_hook_disabled_with_file(hook_name, file) {
return Ok(()); // Already disabled.
}
if let Some(parent) = file.parent() {
let _ = std::fs::create_dir_all(parent);
}
use std::io::Write;
let mut f = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(file)
.map_err(|e| format!("failed to open disabled-hooks file: {e}"))?;
writeln!(f, "{hook_name}").map_err(|e| format!("failed to write disabled-hooks file: {e}"))?;
Ok(())
}
/// Enable a hook by name (remove from ).
pub fn enable_hook(hook_name: &str) -> Result<bool, String> {
match disabled_hooks_file_path() {
Some(file) => enable_hook_with_file(hook_name, &file),
None => Ok(false),
}
}
fn enable_hook_with_file(hook_name: &str, file: &Path) -> Result<bool, String> {
let content = match std::fs::read_to_string(file) {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(e) => return Err(format!("failed to read disabled-hooks file: {e}")),
};
let mut found = false;
let new_lines: Vec<&str> = content
.lines()
.filter(|line| {
let trimmed = line.trim();
if !trimmed.is_empty() && !trimmed.starts_with('#') && trimmed == hook_name {
found = true;
false
} else {
true
}
})
.collect();
if !found {
return Ok(false);
}
if let Some(parent) = file.parent() {
let _ = std::fs::create_dir_all(parent);
}
use std::io::Write;
let mut f = std::fs::File::create(file)
.map_err(|e| format!("failed to open disabled-hooks file: {e}"))?;
for line in new_lines {
writeln!(f, "{line}").map_err(|e| format!("failed to write disabled-hooks file: {e}"))?;
}
Ok(true)
}
/// Returns the path to `$KIGI_SHARE_DIR/disabled-hooks`, or `None` when no user kigi
/// home resolves.
fn disabled_hooks_file_path() -> Option<PathBuf> {
Some(kigi_config::user_kigi_home()?.join("disabled-hooks"))
}
#[cfg(test)]
mod tests {
use super::*;
/// Each test creates its own legacy file in its own temp dir -- no shared state.
fn trust_file_in(dir: &Path) -> PathBuf {
let kigi_dir = dir.join(".kigi");
std::fs::create_dir_all(&kigi_dir).unwrap();
kigi_dir.join("trusted-hook-projects")
}
#[test]
fn list_trusted_projects_parses_paths_skipping_comments_and_blanks() {
let home = tempfile::tempdir().unwrap();
let trust_file = trust_file_in(home.path());
std::fs::write(
&trust_file,
"# comment\n\n/abs/project/one\n /abs/project/two \n# trailing\n",
)
.unwrap();
let projects = list_trusted_projects_with_file(&trust_file).unwrap();
assert_eq!(
projects,
vec![
PathBuf::from("/abs/project/one"),
PathBuf::from("/abs/project/two"),
]
);
}
#[test]
fn list_trusted_projects_missing_file_is_empty() {
// A missing file is Ok(empty), NOT an error — so the migration treats it
// as "nothing to migrate" rather than as an unreadable file.
let projects =
list_trusted_projects_with_file(Path::new("/nonexistent/trusted-hook-projects"))
.expect("missing file resolves to Ok(empty)");
assert!(projects.is_empty());
}
}