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).
268 lines
9.5 KiB
Rust
268 lines
9.5 KiB
Rust
//! Controllable raw HTTP/1.1 artifact server shared by the blitz
|
|
//! download/install tests and the concurrent-update convergence tests.
|
|
//!
|
|
//! Serves a real executable artifact and can truncate the body, close the
|
|
//! connection early, serve a right-length-but-garbage body, or hang
|
|
//! mid-transfer — for both the parallel byte-range path and the
|
|
//! single-connection path. It also counts body-serving GETs (HEAD probes are
|
|
//! excluded) so tests can assert how many downloads actually happened, and
|
|
//! supports a "slow" mode that widens the race window so concurrent
|
|
//! installers genuinely overlap in flight.
|
|
|
|
use std::io::{Read, Write};
|
|
use std::net::{TcpListener, TcpStream};
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::Duration;
|
|
|
|
/// How the server corrupts (or doesn't) the next download.
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub enum Mode {
|
|
/// Serve the real artifact correctly.
|
|
Full,
|
|
/// Serve a right-length body that exits non-zero (fails the smoke-test).
|
|
Garbage,
|
|
/// Advertise the full length but send only `k` bytes then close the socket
|
|
/// (silent truncation: premature EOF / short range chunk).
|
|
Truncate(usize),
|
|
/// Send `k` bytes then hang, so a client-side timeout cancels mid-transfer.
|
|
Hang(usize),
|
|
}
|
|
|
|
struct ServerState {
|
|
body: Arc<Vec<u8>>,
|
|
mode: Mode,
|
|
}
|
|
|
|
pub struct ArtifactServer {
|
|
addr: std::net::SocketAddr,
|
|
state: Arc<Mutex<ServerState>>,
|
|
shutdown: Arc<AtomicBool>,
|
|
gets: Arc<AtomicUsize>,
|
|
slow: Arc<AtomicBool>,
|
|
}
|
|
|
|
impl ArtifactServer {
|
|
pub fn start(body: Vec<u8>) -> Self {
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
listener.set_nonblocking(true).unwrap();
|
|
let addr = listener.local_addr().unwrap();
|
|
let state = Arc::new(Mutex::new(ServerState {
|
|
body: Arc::new(body),
|
|
mode: Mode::Full,
|
|
}));
|
|
let shutdown = Arc::new(AtomicBool::new(false));
|
|
let gets = Arc::new(AtomicUsize::new(0));
|
|
let slow = Arc::new(AtomicBool::new(false));
|
|
|
|
let st = state.clone();
|
|
let sd = shutdown.clone();
|
|
let gc = gets.clone();
|
|
let sl = slow.clone();
|
|
std::thread::spawn(move || {
|
|
while !sd.load(Ordering::Relaxed) {
|
|
match listener.accept() {
|
|
Ok((stream, _)) => {
|
|
let st = st.clone();
|
|
let sd = sd.clone();
|
|
let gc = gc.clone();
|
|
let sl = sl.clone();
|
|
std::thread::spawn(move || handle_connection(stream, st, sd, gc, sl));
|
|
}
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
|
|
std::thread::sleep(Duration::from_millis(2));
|
|
}
|
|
Err(_) => break,
|
|
}
|
|
}
|
|
});
|
|
|
|
Self {
|
|
addr,
|
|
state,
|
|
shutdown,
|
|
gets,
|
|
slow,
|
|
}
|
|
}
|
|
|
|
pub fn uri(&self) -> String {
|
|
format!("http://{}", self.addr)
|
|
}
|
|
|
|
pub fn set_mode(&self, mode: Mode) {
|
|
self.state.lock().unwrap().mode = mode;
|
|
}
|
|
|
|
/// Number of body-serving GET requests handled so far (HEAD probes from
|
|
/// the parallel-download path are excluded). Tests use this to assert
|
|
/// how many downloads actually happened — e.g. that a sequential updater
|
|
/// converged onto an already-installed binary without re-downloading.
|
|
/// One download may span multiple GETs when the parallel byte-range path
|
|
/// splits it, so tests asserting exact counts use a small artifact
|
|
/// (single-connection path, 1 GET per download).
|
|
pub fn request_count(&self) -> usize {
|
|
self.gets.load(Ordering::Relaxed)
|
|
}
|
|
|
|
/// When enabled, hold each Full/Garbage response open ~500ms before
|
|
/// sending the body. This keeps an installer in flight long enough for
|
|
/// concurrent installers to genuinely overlap even on a heavily loaded
|
|
/// CI host — a too-short hold would let race tests run the installers
|
|
/// back-to-back and never exercise the concurrent window.
|
|
pub fn set_slow(&self, slow: bool) {
|
|
self.slow.store(slow, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
impl Drop for ArtifactServer {
|
|
fn drop(&mut self) {
|
|
self.shutdown.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
/// Parse `Range: bytes=a-b` from a raw request header block (case-insensitive).
|
|
fn parse_range(request: &str) -> Option<(usize, usize)> {
|
|
for line in request.lines() {
|
|
let lower = line.to_ascii_lowercase();
|
|
if let Some(rest) = lower.strip_prefix("range:") {
|
|
let spec = rest.trim().strip_prefix("bytes=")?;
|
|
let (a, b) = spec.split_once('-')?;
|
|
return Some((a.trim().parse().ok()?, b.trim().parse().ok()?));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn handle_connection(
|
|
mut stream: TcpStream,
|
|
state: Arc<Mutex<ServerState>>,
|
|
shutdown: Arc<AtomicBool>,
|
|
gets: Arc<AtomicUsize>,
|
|
slow: Arc<AtomicBool>,
|
|
) {
|
|
// A stream accepted from a non-blocking listener can inherit non-blocking
|
|
// mode; force blocking so large `write_all`s don't short-write on WouldBlock.
|
|
let _ = stream.set_nonblocking(false);
|
|
// Avoid Nagle/delayed-ACK stalls on the header-then-body writes.
|
|
let _ = stream.set_nodelay(true);
|
|
|
|
// Read the request header block (until CRLFCRLF). Bodies are never sent by
|
|
// the client, so headers are all we need.
|
|
let mut buf = Vec::new();
|
|
let mut tmp = [0u8; 1024];
|
|
stream.set_read_timeout(Some(Duration::from_secs(5))).ok();
|
|
loop {
|
|
match stream.read(&mut tmp) {
|
|
Ok(0) => break,
|
|
Ok(n) => {
|
|
buf.extend_from_slice(&tmp[..n]);
|
|
if buf.windows(4).any(|w| w == b"\r\n\r\n") {
|
|
break;
|
|
}
|
|
if buf.len() > 64 * 1024 {
|
|
break;
|
|
}
|
|
}
|
|
Err(_) => return,
|
|
}
|
|
}
|
|
let request = String::from_utf8_lossy(&buf).to_string();
|
|
let is_head = request.starts_with("HEAD");
|
|
// Count only body-serving GETs; the parallel path's HEAD probe is excluded.
|
|
if !is_head {
|
|
gets.fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
let range = parse_range(&request);
|
|
|
|
let (body, mode) = {
|
|
let st = state.lock().unwrap();
|
|
(st.body.clone(), st.mode)
|
|
};
|
|
let total = body.len();
|
|
let body: &[u8] = &body;
|
|
|
|
// Determine the byte slice this request is for, plus the length we will
|
|
// claim in Content-Length.
|
|
let (slice_start, slice_end_excl) = match range {
|
|
Some((a, b)) => (a.min(total), (b + 1).min(total)),
|
|
None => (0, total),
|
|
};
|
|
let claimed_len = slice_end_excl - slice_start;
|
|
|
|
// For truncation/hang, `k` is a GLOBAL cutoff across the whole artifact:
|
|
// a slice that reaches past byte `k` is sent short, so the parallel path's
|
|
// later chunk (or the single-connection body) is the one truncated.
|
|
let send_end = match mode {
|
|
Mode::Truncate(k) | Mode::Hang(k) => slice_end_excl.min(k).max(slice_start),
|
|
_ => slice_end_excl,
|
|
};
|
|
// `payload` is what we actually transmit before any early close; for the
|
|
// truncated modes it may be shorter than the advertised `claimed_len`.
|
|
let payload: Vec<u8> = match mode {
|
|
Mode::Garbage => {
|
|
let mut bad = b"#!/bin/sh\nexit 1\n".to_vec();
|
|
bad.resize(claimed_len, b'\n');
|
|
bad
|
|
}
|
|
_ => body[slice_start..send_end].to_vec(),
|
|
};
|
|
|
|
// Status line + headers. For range requests we answer 206; HEAD is 200.
|
|
let mut head = String::new();
|
|
if range.is_some() && !is_head {
|
|
head.push_str("HTTP/1.1 206 Partial Content\r\n");
|
|
head.push_str(&format!(
|
|
"Content-Range: bytes {}-{}/{}\r\n",
|
|
slice_start,
|
|
slice_end_excl.saturating_sub(1),
|
|
total
|
|
));
|
|
} else {
|
|
head.push_str("HTTP/1.1 200 OK\r\n");
|
|
head.push_str("Accept-Ranges: bytes\r\n");
|
|
}
|
|
// Always advertise the (claimed) full length so a truncated transfer is a
|
|
// genuine premature EOF rather than a short-but-consistent body.
|
|
head.push_str(&format!("Content-Length: {}\r\n", claimed_len));
|
|
head.push_str("Connection: close\r\n\r\n");
|
|
|
|
if stream.write_all(head.as_bytes()).is_err() {
|
|
return;
|
|
}
|
|
if is_head {
|
|
let _ = stream.flush();
|
|
return;
|
|
}
|
|
|
|
match mode {
|
|
Mode::Full | Mode::Garbage => {
|
|
// Hold the connection open longer so concurrent installers
|
|
// genuinely overlap mid-download (see `set_slow`).
|
|
if slow.load(Ordering::Relaxed) {
|
|
std::thread::sleep(Duration::from_millis(500));
|
|
}
|
|
let _ = stream.write_all(&payload);
|
|
}
|
|
Mode::Truncate(_) => {
|
|
// Send the (possibly short) payload then drop the connection without
|
|
// meeting Content-Length — the client sees a premature EOF.
|
|
let _ = stream.write_all(&payload);
|
|
}
|
|
Mode::Hang(_) => {
|
|
let _ = stream.write_all(&payload);
|
|
let _ = stream.flush();
|
|
// Hold the connection open longer than any client-side cancel
|
|
// timeout so the client times out and cancels (a genuine mid-flight
|
|
// cancel rather than a server-side close).
|
|
for _ in 0..30 {
|
|
if shutdown.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
std::thread::sleep(Duration::from_millis(20));
|
|
}
|
|
}
|
|
}
|
|
let _ = stream.flush();
|
|
}
|