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:
ZacharyZhang-NY committed 2026-07-17 05:31:01 -04:00
commit d6c20fc13f
2612 files changed
+1353757

No files matched your search

@@ -0,0 +1,320 @@
//! Tests for login, logout, account switching, and auth-code dispatchers.
use super::*;
// ── agent-bound kinds (bash) ─────────
/// A bash command typed while a turn is RUNNING takes the
/// server-authoritative immediate path (Effect + optimistic echo, no local
/// queue entry).
#[test]
fn bash_while_running_is_server_authoritative() {
let mut app = test_app_with_agent();
let id = AgentId(0);
app.agents.get_mut(&id).unwrap().session.state = AgentState::TurnRunning;
let effects = dispatch(Action::SendBashCommand("ls -la".into()), &mut app);
let pid = match &effects[0] {
Effect::SendBashCommand {
command, prompt_id, ..
} => {
assert_eq!(command, "ls -la");
prompt_id.clone()
}
other => panic!("expected immediate SendBashCommand, got {other:?}"),
};
// Not in the local queue.
assert_eq!(app.agents[&id].session.queue_len(), 0);
// Optimistic echo present with kind="bash".
let q = app
.shared_prompt_queue("test-session")
.expect("echo present");
assert_eq!(q.len(), 1);
assert_eq!(q[0].id, pid);
assert_eq!(q[0].kind, "bash");
assert_eq!(q[0].text, "ls -la");
}
#[test]
fn auth_complete_triggers_bundle_status_fetch() {
let mut app = test_app();
app.auth_state = AuthState::Authenticating {
request_seq: 1,
handle: None,
auth_url: None,
mode: AuthMode::Pending,
};
let effects = dispatch(
Action::TaskComplete(TaskResult::AuthComplete {
request_seq: 1,
meta: None,
}),
&mut app,
);
assert!(matches!(app.auth_state, AuthState::Done));
// Pager only refreshes the on-disk catalog snapshot; the actual
// bundle download now runs inside the shell post-auth.
assert!(
effects
.iter()
.any(|e| matches!(e, Effect::FetchBundleStatus))
);
}
#[test]
fn auth_complete_with_deferred_load_also_fetches_status() {
let mut app = test_app();
app.auth_state = AuthState::Authenticating {
request_seq: 1,
handle: None,
auth_url: None,
mode: AuthMode::Pending,
};
app.deferred_startup.session =
Some(crate::app::session_startup::DeferredSessionStartup::Load {
session_id: "test-session".into(),
session_cwd: None,
chat_kind: false,
});
let effects = dispatch(
Action::TaskComplete(TaskResult::AuthComplete {
request_seq: 1,
meta: None,
}),
&mut app,
);
assert!(
effects
.iter()
.any(|e| matches!(e, Effect::FetchBundleStatus))
);
assert!(
effects
.iter()
.any(|e| matches!(e, Effect::LoadSession { .. }))
);
assert!(app.deferred_startup.session.is_none());
}
/// `/login` from the welcome screen (startup / logged-out) must NOT
/// stash a return view — the normal login-then-load flow is preserved.
#[test]
fn login_from_welcome_does_not_stash_return_view() {
let mut app = test_app();
assert_eq!(app.active_view, ActiveView::Welcome);
dispatch(Action::Login, &mut app);
assert_eq!(app.active_view, ActiveView::Welcome);
assert_eq!(app.auth_return_view, None);
}
/// A second auth-failed turn with no rewindable prompt
/// (`in_flight_prompt == None`) must not clobber the stash from an
/// earlier 401.
#[test]
fn second_auth_failure_does_not_clobber_reauth_stash() {
use crate::scrollback::block::RenderBlock;
let mut app = test_app_with_agent();
let id = AgentId(0);
{
let agent = app.agents.get_mut(&id).unwrap();
agent.reauth_stashed_prompt = Some(crate::app::agent::InFlightPrompt {
text: "first prompt".into(),
images: Vec::new(),
scrollback_entry: crate::scrollback::EntryId::new(0),
chip_elements: Vec::new(),
});
agent
.scrollback
.push_block(RenderBlock::session_event(SessionEvent::ReAuthRequired));
agent.session.state = AgentState::TurnRunning;
agent.turn_started_at = Some(std::time::Instant::now());
agent.session.in_flight_prompt = None;
}
dispatch(
Action::TaskComplete(TaskResult::PromptResponse {
agent_id: id,
result: Err("Unauthorized (401)".to_string()),
http_status: Some(401),
prompt_id: None,
}),
&mut app,
);
assert_eq!(
app.agents[&id]
.reauth_stashed_prompt
.as_ref()
.map(|prompt| prompt.text.as_str()),
Some("first prompt"),
"a None in_flight_prompt must not wipe an earlier stash"
);
}
/// Cancelling a mid-session re-auth drops the stashed prompt so it is
/// not silently resubmitted on a later, unrelated login.
#[test]
fn cancel_login_drops_reauth_stashed_prompt() {
let mut app = test_app_with_agent();
let id = AgentId(0);
app.agents.get_mut(&id).unwrap().reauth_stashed_prompt =
Some(crate::app::agent::InFlightPrompt {
text: "stale".into(),
images: Vec::new(),
scrollback_entry: crate::scrollback::EntryId::new(0),
chip_elements: Vec::new(),
});
dispatch(Action::Login, &mut app);
dispatch(Action::CancelLogin, &mut app);
assert!(
app.agents[&id].reauth_stashed_prompt.is_none(),
"cancelling re-auth must drop the stashed prompt"
);
}
/// Cancelling a mid-session re-auth strips the stale `ReAuthRequired`
/// prompt from scrollback so a later `PromptResponse` cannot re-detect
/// it and re-stash the prompt for silent resubmission.
#[test]
fn cancel_login_strips_reauth_prompt_from_scrollback() {
use crate::scrollback::block::RenderBlock;
let mut app = test_app_with_agent();
let id = AgentId(0);
{
let agent = app.agents.get_mut(&id).unwrap();
agent.reauth_stashed_prompt = Some(crate::app::agent::InFlightPrompt {
text: "stale".into(),
images: Vec::new(),
scrollback_entry: crate::scrollback::EntryId::new(0),
chip_elements: Vec::new(),
});
agent
.scrollback
.push_block(RenderBlock::session_event(SessionEvent::ReAuthRequired));
}
dispatch(Action::Login, &mut app);
dispatch(Action::CancelLogin, &mut app);
let sb = &app.agents[&id].scrollback;
let has_reauth = (0..sb.len()).any(|i| {
matches!(
sb.entry(i).map(|e| &e.block),
Some(RenderBlock::SessionEvent(ev)) if matches!(ev.event, SessionEvent::ReAuthRequired)
)
});
assert!(
!has_reauth,
"cancelling re-auth must strip the stale re-auth prompt from scrollback"
);
}
/// Empty `auth_methods` (preferred_method pin unavailable) must not invent
/// `grok.com` or start an OIDC flow the agent did not advertise.
#[test]
fn login_with_empty_auth_methods_fails_closed() {
let mut app = test_app_with_agent();
app.auth_methods.clear();
app.login_method_id = None;
let effects = dispatch(Action::Login, &mut app);
assert!(
effects.is_empty(),
"must not start Authenticate without an advertised method"
);
assert_eq!(
app.active_view,
ActiveView::Agent(AgentId(0)),
"must stay on the session view"
);
assert!(
matches!(
&app.auth_state,
AuthState::Pending { error: Some(msg) }
if msg.contains("preferred_method=api_key")
),
"must surface pin-unavailable error, got {:?}",
app.auth_state
);
assert!(app.login_method_id.is_none());
}
/// Cancelling a mid-session login returns to the session rather than
/// quitting the app, and clears the stashed view + auth state.
#[test]
fn cancel_login_restores_view() {
let mut app = test_app_with_agent();
dispatch(Action::Login, &mut app);
assert_eq!(app.active_view, ActiveView::Welcome);
let effects = dispatch(Action::CancelLogin, &mut app);
assert!(effects.is_empty(), "cancel is pure state, no effects");
assert_eq!(app.active_view, ActiveView::Agent(AgentId(0)));
assert_eq!(app.auth_return_view, None);
assert!(matches!(app.auth_state, AuthState::Done));
}
/// `CancelLogin` outside a mid-session login is a no-op (must not move
/// off the welcome screen or panic).
#[test]
fn cancel_login_noop_without_stashed_view() {
let mut app = test_app();
let effects = dispatch(Action::CancelLogin, &mut app);
assert!(effects.is_empty());
assert_eq!(app.active_view, ActiveView::Welcome);
assert_eq!(app.auth_return_view, None);
}
#[test]
fn auth_complete_extracts_show_resolved_model_from_meta() {
let mut app = test_app();
app.auth_state = AuthState::Authenticating {
request_seq: 1,
handle: None,
auth_url: None,
mode: AuthMode::Pending,
};
assert!(app.show_resolved_model);
dispatch(
Action::TaskComplete(TaskResult::AuthComplete {
request_seq: 1,
meta: Some(serde_json::json!({ "show_resolved_model": false })),
}),
&mut app,
);
assert!(!app.show_resolved_model);
}
#[test]
fn auth_complete_preserves_show_resolved_model_when_absent() {
let mut app = test_app();
app.show_resolved_model = false;
app.auth_state = AuthState::Authenticating {
request_seq: 1,
handle: None,
auth_url: None,
mode: AuthMode::Pending,
};
dispatch(
Action::TaskComplete(TaskResult::AuthComplete {
request_seq: 1,
meta: Some(serde_json::to_value(kigi_shell::auth::AuthMeta::default()).unwrap()),
}),
&mut app,
);
assert!(!app.show_resolved_model);
}