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,160 @@
use kigi_workspace::permission::Decision;
/// Permission-mode label for the `session.permission_mode_changed` span.
pub(crate) fn permission_mode_label(is_yolo: bool) -> &'static str {
if is_yolo {
"bypassPermissions"
} else {
"default"
}
}
/// Telemetry `source` label for a permission [`Decision`] on the `tool.decision`
/// span. `is_yolo` collapses auto-approvals to `config`. `Decision::Allow`/`Ask`
/// carry no provenance, so a config/policy allow is indistinguishable from a
/// user click — report neutral `allowed` rather than guessing `user_temporary`.
pub(crate) fn permission_decision_source(decision: &Decision, is_yolo: bool) -> &'static str {
match decision {
Decision::PolicyDeny(_) => "config",
Decision::Reject(_) => "user_reject",
Decision::Cancelled => "user_abort",
Decision::FollowupMessage(_) => "user_followup",
Decision::Allow | Decision::Ask if is_yolo => "config",
Decision::Allow | Decision::Ask => "allowed",
}
}
/// Emit an `mcp.server_connection` span. `duration_ms` / `tool_count` /
/// `error_type` are status-specific; pass `None` when not applicable.
pub(crate) fn emit_mcp_connection_span(
status: &str,
server_name: &str,
transport_type: &str,
server_scope: &str,
duration_ms: Option<i64>,
tool_count: Option<i64>,
error_type: Option<&str>,
) {
let span = tracing::info_span!(
"mcp.server_connection",
status,
server_name,
transport_type,
server_scope,
duration_ms = tracing::field::Empty,
tool_count = tracing::field::Empty,
error_type = tracing::field::Empty,
);
if let Some(d) = duration_ms {
span.record("duration_ms", d);
}
if let Some(t) = tool_count {
span.record("tool_count", t);
}
if let Some(e) = error_type {
span.record("error_type", e);
}
span.in_scope(|| {});
}
/// Provenance for `skill.activated`'s `skill_source`: project (under `cwd`),
/// user (under `$HOME`), else bundled. Paths are canonicalized (symlinked cwd
/// like macOS `/tmp` vs `/private/tmp`); when both roots match, the deepest
/// wins, tie (cwd == `$HOME`) → user.
pub(crate) fn skill_source_label(skill_path: &str, cwd: &str) -> &'static str {
let canon = |p: &std::path::Path| dunce::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
let p = canon(std::path::Path::new(skill_path));
let depth_if_under =
|base: std::path::PathBuf| p.starts_with(&base).then(|| base.components().count());
let project = depth_if_under(canon(std::path::Path::new(cwd)));
let user = crate::util::kigi_home::kigi_home()
.parent()
.and_then(|home| depth_if_under(canon(home)));
match (project, user) {
(Some(pd), Some(ud)) if pd > ud => "projectSettings",
(Some(_), Some(_)) => "userSettings",
(Some(_), None) => "projectSettings",
(None, Some(_)) => "userSettings",
(None, None) => "bundled",
}
}
pub(crate) fn format_hook_name(spec: &kigi_hooks::config::HookSpec) -> String {
let scope = spec.name.split(':').next().unwrap_or("unknown");
match spec.configured_matcher.as_deref() {
Some(m) if !m.is_empty() => format!("{scope}:{}:{}", spec.event, m.to_lowercase()),
_ => format!("{scope}:{}", spec.event),
}
}
/// Provenance from the namespace prefix each loader stamps on the spec name:
/// `global/` → user, `project/` → project, `plugin/` → plugin, `agent:` →
/// agent, else unknown. (Source-dir classification was wrong — both global and
/// project dirs contain `/.kigi/`.)
fn format_hook_source(spec: &kigi_hooks::config::HookSpec) -> &'static str {
let name = spec.name.as_str();
if name.starts_with("global/") {
"userSettings"
} else if name.starts_with("project/") {
"projectSettings"
} else if name.starts_with("plugin/") {
"pluginHook"
} else if name.starts_with("agent:") {
"agentHook"
} else {
"unknown"
}
}
/// Per-hook inventory recorded as a `hook.registered` span at session start.
pub(crate) struct HookRegInfo {
pub name: String,
pub event: String,
pub hook_type: String,
pub source: &'static str,
}
impl HookRegInfo {
pub(crate) fn from_spec(spec: &kigi_hooks::config::HookSpec) -> Self {
Self {
name: format_hook_name(spec),
event: spec.event.to_string(),
hook_type: spec.handler_type.clone(),
source: format_hook_source(spec),
}
}
}
/// Emit one `plugin.loaded` span per enabled plugin and one
/// `hook.registered` span per configured hook at session start.
pub(crate) fn emit_session_registration_spans(
plugin_registry: Option<&kigi_agent::plugins::PluginRegistry>,
hooks: &[HookRegInfo],
) {
if let Some(registry) = plugin_registry {
for plugin in registry.enabled_plugins() {
tracing::info_span!(
"plugin.loaded",
plugin_name = %plugin.name,
plugin_version = %plugin.version.as_deref().unwrap_or(""),
plugin_scope = plugin.scope.id_label(),
has_hooks = plugin.has_hooks,
has_mcp = plugin.mcp_server_count > 0,
skill_count = plugin.skill_count as i64,
agent_count = plugin.agent_count as i64,
command_path_count = plugin.command_dirs.len() as i64,
)
.in_scope(|| {});
}
}
for h in hooks {
tracing::info_span!(
"hook.registered",
hook_name = %h.name,
hook_event = %h.event,
hook_type = %h.hook_type,
hook_source = %h.source,
)
.in_scope(|| {});
}
}