M0: compilable skeleton — Kigi 0.1.0 fork surgery

Hard fork of xai-org/grok-build (Apache-2.0) re-targeted as Kigi, an
unofficial Kimi Code CLI community build.

Rename & identity
- 72 xai-*/xai-grok-* crates -> kigi-* (explicit: xai-grok-pager-bin ->
  kigi-bin [binary `kigi`], xai-grok-pager -> kigi-tui; rest mechanical);
  ptyctl, ptyctl-cli, third_party/ unchanged; proto package
  xai.grok.tools.v1 -> kigi.tools.v1
- Config home ~/.kigi (KIGI_SHARE_DIR override), env prefix GROK_* ->
  KIGI_*, `kigi --version` carries the unofficial-community-build notice
- clap identity, help text, startup banner, prompt templates rebranded
  (templates re-encrypted)

Deletions (PRD removal list #5/#6/#7/#9/#10)
- voice input (xai-grok-voice) and all TUI wiring
- telemetry: Mixpanel client, external OTel stream, Sentry, OTLP layers,
  trace/GCS/S3 upload queues (kigi-file-utils halved), workspace upload
  module & dc_log, heap-profile uploader, auth-diagnostics uploader,
  session-analytics halves of feedback; local zero-egress observability
  preserved in new kigi-log crate (unified log, --debug firehose,
  subsystem file logs, opt-in instrumentation)
- announcements (crate, remote-settings fields, TUI surfaces)
- plugin marketplace (crate, sources/browse/CTA/extensions-modal tab);
  direct plugin install/uninstall/update via kigi-agent git_install kept
- relay/gateway/assets endpoints and features (agent relay, headless
  relay transport, gateway bridge, LeaderEnvUrls); leader IPC socket now
  ~/.kigi/leader.sock + KIGI_LEADER_SOCKET, no ws-url derivation
- functional types rehomed instead of deleted: PermissionMode ->
  kigi-config-types, McpInitStrategy -> kigi-mcp, PrCreationSource ->
  session signals, TerminalDiagnostics -> kigi-pager-render, agent_id ->
  shell util

Endpoints
- kigi-env rewritten: single production KigiEndpoints {coding_api_base_url
  https://api.kimi.com/coding/v1 (KIGI_CODE_BASE_URL), oauth_host
  https://auth.kimi.com (KIGI_OAUTH_HOST), update_base_url (GitHub
  Releases API), upgrade_page_url}; GrokBuildEnvironment enum deleted

Toolchain & workspace hygiene
- Rust 1.97.0 pinned; edition 2024; full cargo update; git2 hoisted to
  workspace at 0.21 (Option->Result API migration), quick-xml 0.41
- Root Cargo.toml hand-maintained (PRD §8.1): version 0.1.0 inherited by
  all members, members sorted, unused deps pruned
- cargo-deny advisories gate (deny.toml with documented transitive
  exceptions); CI workflow (check/clippy/fmt/deny/test, macOS+Linux)
- cross-crate test seams re-gated behind `test-support` cargo feature;
  insta snapshot baselines renamed to the kigi_tui prefix
- clippy --workspace --all-targets: zero warnings; fmt clean

Fixes surfaced by the port
- updater probe/installer divergence (bin/kigi vs bin/grok symlink set)
- idle model-metadata refresh dead under KIGI_CODE_BASE_URL override
  (new is_effective_coding_endpoint_url, loopback+override aware)
- macOS symlinked-TMPDIR fixture canonicalization (foreign_sessions,
  fast-worktree); RSS measurement tests serialized via serial_test

Docs & legal (Apache §4)
- NOTICE added (upstream attribution + change statement); THIRD-PARTY
  notices sustained; kigi-tools ported-code notices extended; README,
  CONTRIBUTING, SECURITY, AGENTS.md rewritten

Out of scope for M0 (tracked): Kimi auth/inference (M1), search/fetch,
command parity, config import (M2), Computer Hub excision & final
brand-token sweep (M2), distribution & self-update rewrite (M3).
This commit is contained in:
2026-07-17 05:31:01 -04:00
commit d6c20fc13f
2612 changed files with 1353757 additions and 0 deletions
@@ -0,0 +1,162 @@
//! Optional multi-user workspace helpers for loading per-user agent config.
//!
//! When optional workspace root and user env vars are set and the resolved
//! directory exists, that path can contribute AGENTS.md / rules / skills
//! discovery. Unset env vars are a no-op (typical for standalone installs).
use std::path::PathBuf;
/// If optional workspace env vars are set, returns the user's config directory
/// when the resolved path exists on disk. Unset or missing paths yield `None`.
pub fn optional_workspace_user_dir() -> Option<PathBuf> {
let root = std::env::var("XAI_ROOT").ok()?;
let user = std::env::var("XAI_USER").ok()?;
resolve_workspace_user_dir(&root, &workspace_user_relpath(&user))
}
/// Map `$XAI_USER` to a path relative to the workspace root.
///
/// A bare username is nested one level under `x/` so it cannot collide with an
/// unrelated same-named directory at the workspace root. Values that already
/// contain a path separator are used as-is (explicit relative path).
fn workspace_user_relpath(user: &str) -> String {
if user.contains('/') || user.contains('\\') {
user.to_string()
} else {
format!("x/{user}")
}
}
/// Pure logic: join `root` with a relative `user` path and return it if the
/// directory exists on disk.
///
/// Returns `None` if either argument is empty or the resulting path is not
/// a directory.
///
/// Example: `resolve_workspace_user_dir("/workspace", "users/alice")`
/// → `Some("/workspace/users/alice")` if that directory exists.
pub fn resolve_workspace_user_dir(root: &str, user: &str) -> Option<PathBuf> {
if root.is_empty() || user.is_empty() {
return None;
}
let path = PathBuf::from(root).join(user);
path.is_dir().then_some(path)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
// ── resolve_workspace_user_dir (pure, no env vars) ───────────────
#[test]
fn resolve_returns_none_for_empty_root() {
assert!(resolve_workspace_user_dir("", "users/someone").is_none());
}
#[test]
fn resolve_returns_none_for_empty_user() {
let tmp = tempfile::tempdir().unwrap();
assert!(resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "").is_none());
}
#[test]
fn resolve_returns_none_for_both_empty() {
assert!(resolve_workspace_user_dir("", "").is_none());
}
#[test]
fn resolve_returns_none_when_dir_does_not_exist() {
let tmp = tempfile::tempdir().unwrap();
assert!(
resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "users/nonexistent").is_none()
);
}
#[test]
fn resolve_returns_path_when_dir_exists() {
let tmp = tempfile::tempdir().unwrap();
let user_dir = tmp.path().join("users").join("testuser");
fs::create_dir_all(&user_dir).unwrap();
let result = resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "users/testuser");
assert_eq!(result, Some(user_dir));
}
#[test]
fn resolve_handles_single_component_user() {
let tmp = tempfile::tempdir().unwrap();
let user_dir = tmp.path().join("alice");
fs::create_dir_all(&user_dir).unwrap();
let result = resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "alice");
assert_eq!(result, Some(user_dir));
}
#[test]
fn resolve_handles_deeply_nested_user() {
let tmp = tempfile::tempdir().unwrap();
let user_dir = tmp.path().join("org").join("team").join("user");
fs::create_dir_all(&user_dir).unwrap();
let result = resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "org/team/user");
assert_eq!(result, Some(user_dir));
}
#[test]
fn resolve_returns_none_when_path_is_file_not_dir() {
let tmp = tempfile::tempdir().unwrap();
let file_path = tmp.path().join("users").join("testuser");
fs::create_dir_all(file_path.parent().unwrap()).unwrap();
fs::write(&file_path, "not a directory").unwrap();
assert!(
resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "users/testuser").is_none()
);
}
#[test]
fn resolve_supports_nested_user_layout_path() {
let tmp = tempfile::tempdir().unwrap();
let user_dir = tmp.path().join("x").join("testuser");
fs::create_dir_all(&user_dir).unwrap();
let result = resolve_workspace_user_dir(tmp.path().to_str().unwrap(), "x/testuser");
assert_eq!(result, Some(user_dir));
}
// ── workspace_user_relpath ───────────────────────────────────────
#[test]
fn bare_username_is_nested_under_x() {
assert_eq!(workspace_user_relpath("alice"), "x/alice");
assert_eq!(workspace_user_relpath("bob"), "x/bob");
}
#[test]
fn multi_segment_user_is_explicit_relative_path() {
assert_eq!(workspace_user_relpath("users/alice"), "users/alice");
assert_eq!(workspace_user_relpath(r"users\alice"), r"users\alice");
}
#[test]
fn bare_username_does_not_resolve_to_same_named_root_dir() {
// Prefer the nested layout even when a same-named directory exists at
// the workspace root.
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
fs::create_dir_all(root.join("alice")).unwrap();
let user_dir = root.join("x").join("alice");
fs::create_dir_all(&user_dir).unwrap();
let rel = workspace_user_relpath("alice");
let resolved = resolve_workspace_user_dir(root.to_str().unwrap(), &rel);
assert_eq!(resolved, Some(user_dir));
assert_ne!(
resolved.as_deref(),
Some(root.join("alice").as_path()),
"must not resolve to a same-named directory at the workspace root"
);
}
}