Files
Kigi-CLI/crates/codegen/kigi-tui/src/config_toml_edit.rs
T
ZacharyZhang-NY d6c20fc13f 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).
2026-07-17 05:31:01 -04:00

182 lines
6.1 KiB
Rust

//! Load `config.toml` as a [`toml_edit::DocumentMut`] for in-place edits.
//! A non-empty file that does not parse is left untouched (`None`).
use std::path::Path;
#[must_use]
pub(crate) fn read_config_document_for_edit(path: &Path) -> Option<toml_edit::DocumentMut> {
#[allow(clippy::manual_unwrap_or_default)]
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => String::new(),
};
match content.parse() {
Ok(d) => Some(d),
Err(e) => {
if content.is_empty() {
return Some(toml_edit::DocumentMut::new());
}
tracing::warn!(
path = %path.display(),
error = %e,
"config.toml is not valid TOML; refusing to overwrite"
);
None
}
}
}
/// Set `[hints].<key>` to `value` in `~/.kigi/config.toml`, preserving every
/// other key and table. Creates the file and parent dir when missing, and
/// no-ops when the existing file is non-empty but unparseable (so a malformed
/// config is never clobbered). Performs blocking I/O.
pub(crate) fn set_hint(key: &str, value: impl Into<toml_edit::Value>) -> std::io::Result<()> {
let path = kigi_tools::util::kigi_home::kigi_home().join("config.toml");
set_hint_at(&path, key, value)
}
/// Path-injectable core of [`set_hint`].
fn set_hint_at(path: &Path, key: &str, value: impl Into<toml_edit::Value>) -> std::io::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let Some(mut doc) = read_config_document_for_edit(path) else {
return Ok(());
};
doc["hints"][key] = toml_edit::value(value);
std::fs::write(path, doc.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn merge_round_trip_preserves_sibling_tables() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
fs::write(
&path,
"[ui]\ncompact_mode = false\n\n[mcpServers]\nx = \"y\"\n",
)
.unwrap();
let mut doc = read_config_document_for_edit(&path).expect("parse");
doc["ui"]["show_timestamps"] = toml_edit::value(false);
fs::write(&path, doc.to_string()).unwrap();
let body = fs::read_to_string(&path).unwrap();
assert!(
body.contains("show_timestamps") && body.contains("mcpServers"),
"expected merged TOML, got:\n{body}"
);
}
#[test]
fn nonempty_unparseable_returns_none_and_leaves_file() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
let bad = "this is [not valid toml\n";
fs::write(&path, bad).unwrap();
assert!(read_config_document_for_edit(&path).is_none());
assert_eq!(fs::read_to_string(&path).unwrap(), bad);
}
#[test]
fn missing_file_is_editable_empty_doc() {
let dir = tempdir().unwrap();
let path = dir.path().join("absent.toml");
let doc = read_config_document_for_edit(&path).expect("editable");
assert!(!doc.contains_key("ui"));
}
#[test]
fn set_hint_at_round_trips_and_preserves_siblings() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
fs::write(&path, "[ui]\ncompact_mode = false\n").unwrap();
set_hint_at(&path, "project_picker_disabled", true).unwrap();
let doc = read_config_document_for_edit(&path).expect("reparse");
assert_eq!(
doc.get("hints")
.and_then(|h| h.get("project_picker_disabled"))
.and_then(|v| v.as_bool()),
Some(true),
);
assert!(
fs::read_to_string(&path).unwrap().contains("compact_mode"),
"sibling [ui] should be preserved"
);
}
#[test]
fn set_hint_at_creates_missing_file() {
let dir = tempdir().unwrap();
let path = dir.path().join("nested/config.toml");
set_hint_at(&path, "project_picker_disabled", true).unwrap();
assert!(
path.exists(),
"missing file and parent dir should be created"
);
}
#[test]
fn set_hint_write_then_read_back_round_trips() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
fs::write(&path, "[ui]\ntheme = \"dark\"\n").unwrap();
set_hint_at(&path, "project_picker_disabled", true).unwrap();
let doc = read_config_document_for_edit(&path).expect("reparse");
let disabled = doc
.get("hints")
.and_then(|h| h.get("project_picker_disabled"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
assert!(disabled, "should read back true after set_hint write");
}
#[test]
fn set_hint_at_leaves_unparseable_file_untouched() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
let bad = "this is [not valid toml\n";
fs::write(&path, bad).unwrap();
// No-op (no write, no clobber) when the existing file cannot be parsed.
set_hint_at(&path, "project_picker_disabled", true).unwrap();
assert_eq!(fs::read_to_string(&path).unwrap(), bad);
}
#[test]
fn vim_mode_round_trip() {
let dir = tempdir().unwrap();
let path = dir.path().join("config.toml");
fs::write(&path, "[ui]\ncompact_mode = false\n").unwrap();
let mut doc = read_config_document_for_edit(&path).expect("parse");
doc["ui"]["vim_mode"] = toml_edit::value(true);
fs::write(&path, doc.to_string()).unwrap();
let doc2 = read_config_document_for_edit(&path).expect("reparse");
let enabled = doc2
.get("ui")
.and_then(|h| h.get("vim_mode"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
assert!(enabled, "expected vim_mode = true after round-trip");
let body = fs::read_to_string(&path).unwrap();
assert!(
body.contains("compact_mode"),
"sibling [ui] keys should be preserved"
);
}
}