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,108 @@
//! Detects whether grok is running inside an editor's embedded `:terminal`
//! (Neovim/Vim `:terminal`, Emacs `vterm`).
//!
//! WHY this matters: inside an editor `:terminal` the *immediate* terminal
//! emulator is the editor's own libvterm, not tmux — even though the `TMUX`
//! env var is inherited through the editor. A tmux DCS passthrough envelope
//! (`\x1bPtmux;…\x1b\\`) is only understood by tmux, so emitting it into the
//! editor's libvterm renders the wrapper as visible garbage text. Detection
//! records this on [`super::TerminalContext`] so clipboard routing emits a
//! plain OSC 52 sequence in that case instead.
use std::collections::HashMap;
use super::env_get;
/// Which embedded editor `:terminal` grok is running inside.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EmbeddedEditor {
/// Neovim `:terminal` (sets `NVIM`, or legacy `NVIM_LISTEN_ADDRESS`).
Neovim,
/// Vim 8/9 `:terminal` (sets `VIM_TERMINAL`).
Vim,
/// Emacs (sets `INSIDE_EMACS`; `vterm` uses libvterm — same bug).
Emacs,
}
/// Detect the embedded editor terminal from an injected environment map.
///
/// Checks, in order: `NVIM` / `NVIM_LISTEN_ADDRESS` → [`EmbeddedEditor::Neovim`];
/// `VIM_TERMINAL` → [`EmbeddedEditor::Vim`]; `INSIDE_EMACS` →
/// [`EmbeddedEditor::Emacs`]; else `None`. Empty values are treated as absent
/// (matching the sibling `detect_*_from_env` detectors via `env_get`).
///
/// Adding a new env marker here requires extending
/// `HOST_TERMINAL_ENV_VARS` in `kigi-pager-pty-harness/src/pty.rs`
/// (test-env hygiene).
pub fn embedded_editor_from_env(env: &HashMap<String, String>) -> Option<EmbeddedEditor> {
// Markers can't distinguish editor-inside-tmux (the 100%-repro bug; don't wrap)
// from the inverted tmux-inside-editor; we target the former and the latter still
// works since plain OSC 52 is forwarded.
// NVIM_LISTEN_ADDRESS is legacy (modern nvim unsets it at startup); a
// stray/user-exported marker only degrades to plain OSC 52, never garbage.
if env_get(env, "NVIM").is_some() || env_get(env, "NVIM_LISTEN_ADDRESS").is_some() {
return Some(EmbeddedEditor::Neovim);
}
if env_get(env, "VIM_TERMINAL").is_some() {
return Some(EmbeddedEditor::Vim);
}
if env_get(env, "INSIDE_EMACS").is_some() {
return Some(EmbeddedEditor::Emacs);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::terminal::env_from;
#[test]
fn nvim_detected_as_neovim() {
let env = env_from(&[("NVIM", "/tmp/nvim.12345.0")]);
assert_eq!(embedded_editor_from_env(&env), Some(EmbeddedEditor::Neovim));
}
#[test]
fn nvim_listen_address_detected_as_neovim() {
let env = env_from(&[("NVIM_LISTEN_ADDRESS", "/tmp/nvim.sock")]);
assert_eq!(embedded_editor_from_env(&env), Some(EmbeddedEditor::Neovim));
}
#[test]
fn vim_terminal_detected_as_vim() {
let env = env_from(&[("VIM_TERMINAL", "8.2")]);
assert_eq!(embedded_editor_from_env(&env), Some(EmbeddedEditor::Vim));
}
#[test]
fn inside_emacs_detected_as_emacs() {
let env = env_from(&[("INSIDE_EMACS", "30.1,vterm")]);
assert_eq!(embedded_editor_from_env(&env), Some(EmbeddedEditor::Emacs));
}
#[test]
fn no_editor_markers_is_none() {
let env = env_from(&[
("TERM", "xterm-256color"),
("TMUX", "/tmp/tmux-501/default,1,0"),
]);
assert_eq!(embedded_editor_from_env(&env), None);
}
#[test]
fn empty_value_treated_as_absent() {
let env = env_from(&[("NVIM", ""), ("VIM_TERMINAL", ""), ("INSIDE_EMACS", "")]);
assert_eq!(embedded_editor_from_env(&env), None);
}
#[test]
fn nvim_beats_vim_and_emacs() {
let env = env_from(&[
("NVIM", "/tmp/nvim.12345.0"),
("VIM_TERMINAL", "8.2"),
("INSIDE_EMACS", "30.1,vterm"),
]);
assert_eq!(embedded_editor_from_env(&env), Some(EmbeddedEditor::Neovim));
}
}