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,249 @@
//! Memory benchmark for SubagentInfo Arc<str> optimization.
//!
//! This benchmark measures the memory savings from using Arc<str> instead of String
//! for SubagentInfo fields. It creates many SubagentInfo instances with shared string
//! values and measures the memory usage.
//!
//! Run with: cargo run --release --example memory_benchmark
// Illustrative mock structs whose fields exist to model memory layout; not all
// are read back, which is expected for a microbenchmark.
#![allow(dead_code)]
use std::sync::Arc;
use std::time::Instant;
/// Simulate SubagentInfo with String fields (before optimization)
#[derive(Clone)]
struct SubagentInfoString {
subagent_id: String,
child_session_id: String,
description: String,
subagent_type: String,
persona: Option<String>,
role: Option<String>,
model: Option<String>,
status: Option<String>,
tools_used: Vec<String>,
}
/// Simulate SubagentInfo with Arc<str> fields (after optimization)
#[derive(Clone)]
struct SubagentInfoArc {
subagent_id: Arc<str>,
child_session_id: Arc<str>,
description: Arc<str>,
subagent_type: Arc<str>,
persona: Option<Arc<str>>,
role: Option<Arc<str>>,
model: Option<Arc<str>>,
status: Option<Arc<str>>,
tools_used: Vec<Arc<str>>,
}
fn create_string_info(id: usize) -> SubagentInfoString {
SubagentInfoString {
subagent_id: format!("sa-{}", id),
child_session_id: format!("cs-{}", id),
description: "Find API endpoints in the codebase".to_string(),
subagent_type: "general-purpose".to_string(),
persona: Some("researcher".to_string()),
role: Some("analyst".to_string()),
model: Some("grok-3".to_string()),
status: Some("completed".to_string()),
tools_used: vec!["read".to_string(), "search".to_string(), "edit".to_string()],
}
}
fn create_arc_info(id: usize) -> SubagentInfoArc {
SubagentInfoArc {
subagent_id: Arc::from(format!("sa-{}", id)),
child_session_id: Arc::from(format!("cs-{}", id)),
description: Arc::from("Find API endpoints in the codebase"),
subagent_type: Arc::from("general-purpose"),
persona: Some(Arc::from("researcher")),
role: Some(Arc::from("analyst")),
model: Some(Arc::from("grok-3")),
status: Some(Arc::from("completed")),
tools_used: vec![Arc::from("read"), Arc::from("search"), Arc::from("edit")],
}
}
fn estimate_string_info_size(info: &SubagentInfoString) -> usize {
std::mem::size_of::<SubagentInfoString>()
+ info.subagent_id.capacity()
+ info.child_session_id.capacity()
+ info.description.capacity()
+ info.subagent_type.capacity()
+ info.persona.as_ref().map_or(0, |s| s.capacity())
+ info.role.as_ref().map_or(0, |s| s.capacity())
+ info.model.as_ref().map_or(0, |s| s.capacity())
+ info.status.as_ref().map_or(0, |s| s.capacity())
+ info.tools_used.iter().map(|s| s.capacity()).sum::<usize>()
}
fn estimate_arc_info_size(info: &SubagentInfoArc) -> usize {
// Arc<str> has 16 bytes overhead (fat pointer) but shares the string data
std::mem::size_of::<SubagentInfoArc>()
+ 16 // subagent_id Arc overhead
+ 16 // child_session_id Arc overhead
+ 16 // description Arc overhead
+ 16 // subagent_type Arc overhead
+ info.persona.as_ref().map_or(0, |_| 16)
+ info.role.as_ref().map_or(0, |_| 16)
+ info.model.as_ref().map_or(0, |_| 16)
+ info.status.as_ref().map_or(0, |_| 16)
+ info.tools_used.len() * 16
}
fn main() {
println!("=== SubagentInfo Memory Benchmark ===\n");
// Test 1: Single instance
println!("--- Single Instance ---");
let string_info = create_string_info(1);
let arc_info = create_arc_info(1);
println!(
"String-based SubagentInfo size: ~{} bytes",
estimate_string_info_size(&string_info)
);
println!(
"Arc<str>-based SubagentInfo size: ~{} bytes",
estimate_arc_info_size(&arc_info)
);
println!();
// Test 2: Many instances with shared strings
println!("--- 1000 Instances (with string sharing potential) ---");
let count = 1000;
// String-based: each instance has its own copy of shared strings
let string_infos: Vec<SubagentInfoString> = (0..count).map(create_string_info).collect();
// Arc-based: shared strings are deduplicated
let arc_infos: Vec<SubagentInfoArc> = (0..count).map(create_arc_info).collect();
let string_total: usize = string_infos.iter().map(estimate_string_info_size).sum();
let arc_total: usize = arc_infos.iter().map(estimate_arc_info_size).sum();
println!(
"String-based total: {} bytes ({:.1} KB)",
string_total,
string_total as f64 / 1024.0
);
println!(
"Arc<str>-based total: {} bytes ({:.1} KB)",
arc_total,
arc_total as f64 / 1024.0
);
println!(
"Savings: {} bytes ({:.1} KB, {:.1}%)",
string_total - arc_total,
(string_total - arc_total) as f64 / 1024.0,
(string_total - arc_total) as f64 / string_total as f64 * 100.0
);
println!();
// Test 3: Clone performance
println!("--- Clone Performance (100,000 clones) ---");
let iterations = 100_000;
let start = Instant::now();
for _ in 0..iterations {
let _ = string_info.clone();
}
let string_clone_time = start.elapsed();
let start = Instant::now();
for _ in 0..iterations {
let _ = arc_info.clone();
}
let arc_clone_time = start.elapsed();
println!("String clone time: {:?}", string_clone_time);
println!("Arc<str> clone time: {:?}", arc_clone_time);
println!(
"Speedup: {:.1}x",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
println!();
// Test 4: Memory with realistic subagent data
println!("--- Realistic Scenario (100 subagents with varied data) ---");
let subagent_types = ["general-purpose", "explore", "plan", "implementer"];
let personas = ["researcher", "analyst", "reviewer", "implementer"];
let models = ["grok-3", "grok-3-mini", "grok-4"];
let statuses = ["completed", "failed", "running", "cancelled"];
let tools = ["read", "edit", "search", "execute", "list_dir"];
let mut string_infos: Vec<SubagentInfoString> = Vec::new();
let mut arc_infos: Vec<SubagentInfoArc> = Vec::new();
for i in 0..100 {
let st = subagent_types[i % subagent_types.len()];
let p = personas[i % personas.len()];
let m = models[i % models.len()];
let s = statuses[i % statuses.len()];
string_infos.push(SubagentInfoString {
subagent_id: format!("sa-{}", i),
child_session_id: format!("cs-{}", i),
description: format!("Task {}: analyze the codebase", i),
subagent_type: st.to_string(),
persona: Some(p.to_string()),
role: Some(p.to_string()),
model: Some(m.to_string()),
status: Some(s.to_string()),
tools_used: tools
.iter()
.take(i % 5 + 1)
.map(|t| t.to_string())
.collect(),
});
arc_infos.push(SubagentInfoArc {
subagent_id: Arc::from(format!("sa-{}", i)),
child_session_id: Arc::from(format!("cs-{}", i)),
description: Arc::from(format!("Task {}: analyze the codebase", i)),
subagent_type: Arc::from(st),
persona: Some(Arc::from(p)),
role: Some(Arc::from(p)),
model: Some(Arc::from(m)),
status: Some(Arc::from(s)),
tools_used: tools
.iter()
.take(i % 5 + 1)
.map(|t| Arc::from(*t))
.collect(),
});
}
let string_total: usize = string_infos.iter().map(estimate_string_info_size).sum();
let arc_total: usize = arc_infos.iter().map(estimate_arc_info_size).sum();
println!(
"String-based total: {} bytes ({:.1} KB)",
string_total,
string_total as f64 / 1024.0
);
println!(
"Arc<str>-based total: {} bytes ({:.1} KB)",
arc_total,
arc_total as f64 / 1024.0
);
println!(
"Savings: {} bytes ({:.1} KB, {:.1}%)",
string_total - arc_total,
(string_total - arc_total) as f64 / 1024.0,
(string_total - arc_total) as f64 / string_total as f64 * 100.0
);
println!();
// Summary
println!("=== Summary ===");
println!("Arc<str> optimization provides:");
println!("1. Memory savings through string deduplication (shared strings stored once)");
println!("2. Faster cloning (O(1) refcount increment vs O(n) string copy)");
println!("3. Better cache locality for frequently accessed shared strings");
}
@@ -0,0 +1,275 @@
//! Accurate memory benchmark for SubagentInfo Arc<str> optimization.
//!
//! This benchmark uses dhat for heap profiling to accurately measure memory usage.
//!
//! Run with: cargo run --release --example memory_benchmark_accurate
// Illustrative mock structs whose fields exist to model memory layout; not all
// are read back, which is expected for a microbenchmark.
#![allow(dead_code)]
use std::sync::Arc;
use std::time::Instant;
/// Simulate SubagentInfo with String fields (before optimization)
#[derive(Clone)]
struct SubagentInfoString {
subagent_id: String,
child_session_id: String,
description: String,
subagent_type: String,
persona: Option<String>,
role: Option<String>,
model: Option<String>,
status: Option<String>,
tools_used: Vec<String>,
}
/// Simulate SubagentInfo with Arc<str> fields (after optimization)
#[derive(Clone)]
struct SubagentInfoArc {
subagent_id: Arc<str>,
child_session_id: Arc<str>,
description: Arc<str>,
subagent_type: Arc<str>,
persona: Option<Arc<str>>,
role: Option<Arc<str>>,
model: Option<Arc<str>>,
status: Option<Arc<str>>,
tools_used: Vec<Arc<str>>,
}
fn create_string_info(
id: usize,
shared_type: &str,
shared_model: &str,
shared_persona: &str,
) -> SubagentInfoString {
SubagentInfoString {
subagent_id: format!("sa-{}", id),
child_session_id: format!("cs-{}", id),
description: format!("Task {}: analyze the codebase for API endpoints", id),
subagent_type: shared_type.to_string(),
persona: Some(shared_persona.to_string()),
role: Some(shared_persona.to_string()),
model: Some(shared_model.to_string()),
status: Some("completed".to_string()),
tools_used: vec!["read".to_string(), "search".to_string(), "edit".to_string()],
}
}
fn create_arc_info(
id: usize,
shared_type: &str,
shared_model: &str,
shared_persona: &str,
) -> SubagentInfoArc {
SubagentInfoArc {
subagent_id: Arc::from(format!("sa-{}", id)),
child_session_id: Arc::from(format!("cs-{}", id)),
description: Arc::from(format!(
"Task {}: analyze the codebase for API endpoints",
id
)),
subagent_type: Arc::from(shared_type),
persona: Some(Arc::from(shared_persona)),
role: Some(Arc::from(shared_persona)),
model: Some(Arc::from(shared_model)),
status: Some(Arc::from("completed")),
tools_used: vec![Arc::from("read"), Arc::from("search"), Arc::from("edit")],
}
}
fn main() {
println!("=== SubagentInfo Memory Benchmark (Accurate) ===\n");
// Test 1: Clone performance - the most impactful optimization
println!("--- Clone Performance (1,000,000 clones) ---");
let iterations = 1_000_000;
let string_info = create_string_info(1, "general-purpose", "grok-3", "researcher");
let arc_info = create_arc_info(1, "general-purpose", "grok-3", "researcher");
let start = Instant::now();
for _ in 0..iterations {
let _ = string_info.clone();
}
let string_clone_time = start.elapsed();
let start = Instant::now();
for _ in 0..iterations {
let _ = arc_info.clone();
}
let arc_clone_time = start.elapsed();
println!("String clone time: {:?}", string_clone_time);
println!("Arc<str> clone time: {:?}", arc_clone_time);
println!(
"Speedup: {:.1}x",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
println!();
// Test 2: Memory with shared strings (realistic scenario)
println!("--- Memory with Shared Strings (1000 subagents) ---");
println!("Scenario: 1000 subagents sharing subagent_type, model, persona, status");
let shared_types = ["general-purpose", "explore", "plan"];
let shared_models = ["grok-3", "grok-3-mini"];
let shared_personas = ["researcher", "analyst", "reviewer"];
// Create string-based infos
let mut string_infos: Vec<SubagentInfoString> = Vec::with_capacity(1000);
for i in 0..1000 {
let st = shared_types[i % shared_types.len()];
let m = shared_models[i % shared_models.len()];
let p = shared_personas[i % shared_personas.len()];
string_infos.push(create_string_info(i, st, m, p));
}
// Create Arc-based infos (with string sharing)
let mut arc_infos: Vec<SubagentInfoArc> = Vec::with_capacity(1000);
for i in 0..1000 {
let st = shared_types[i % shared_types.len()];
let m = shared_models[i % shared_models.len()];
let p = shared_personas[i % shared_personas.len()];
arc_infos.push(create_arc_info(i, st, m, p));
}
// Calculate memory
// For String: each instance has its own copy
let string_mem: usize = string_infos
.iter()
.map(|info| {
info.subagent_id.capacity()
+ info.child_session_id.capacity()
+ info.description.capacity()
+ info.subagent_type.capacity()
+ info.persona.as_ref().map_or(0, |s| s.capacity())
+ info.role.as_ref().map_or(0, |s| s.capacity())
+ info.model.as_ref().map_or(0, |s| s.capacity())
+ info.status.as_ref().map_or(0, |s| s.capacity())
+ info.tools_used.iter().map(|s| s.capacity()).sum::<usize>()
})
.sum();
// For Arc<str>: shared strings are stored once
// Count unique strings
let mut unique_types = std::collections::HashSet::new();
let mut unique_models = std::collections::HashSet::new();
let mut unique_personas = std::collections::HashSet::new();
let mut unique_statuses = std::collections::HashSet::new();
let mut unique_tools = std::collections::HashSet::new();
for info in &arc_infos {
unique_types.insert(info.subagent_type.as_ref());
if let Some(ref m) = info.model {
unique_models.insert(m.as_ref());
}
if let Some(ref p) = info.persona {
unique_personas.insert(p.as_ref());
}
if let Some(ref s) = info.status {
unique_statuses.insert(s.as_ref());
}
for t in &info.tools_used {
unique_tools.insert(t.as_ref());
}
}
// Arc<str> memory: unique strings + Arc overhead per reference
let shared_string_mem: usize = unique_types.iter().map(|s| s.len()).sum::<usize>()
+ unique_models.iter().map(|s| s.len()).sum::<usize>()
+ unique_personas.iter().map(|s| s.len()).sum::<usize>()
+ unique_statuses.iter().map(|s| s.len()).sum::<usize>()
+ unique_tools.iter().map(|s| s.len()).sum::<usize>();
// Per-instance memory for unique fields + Arc overhead
let per_instance_mem: usize = arc_infos
.iter()
.map(|info| {
info.subagent_id.len() + 16 + // Arc overhead
info.child_session_id.len() + 16 +
info.description.len() + 16 +
16 + // subagent_type Arc (shared)
info.persona.as_ref().map_or(0, |_| 16) + // Arc overhead
info.role.as_ref().map_or(0, |_| 16) +
info.model.as_ref().map_or(0, |_| 16) +
info.status.as_ref().map_or(0, |_| 16) +
info.tools_used.len() * 16
})
.sum();
let arc_mem = shared_string_mem + per_instance_mem;
println!(
"String-based memory: {} bytes ({:.1} KB)",
string_mem,
string_mem as f64 / 1024.0
);
println!(
"Arc<str>-based memory: {} bytes ({:.1} KB)",
arc_mem,
arc_mem as f64 / 1024.0
);
println!(" - Shared strings: {} bytes", shared_string_mem);
println!(" - Per-instance: {} bytes", per_instance_mem);
println!(
"Savings: {} bytes ({:.1} KB, {:.1}%)",
string_mem.saturating_sub(arc_mem),
string_mem.saturating_sub(arc_mem) as f64 / 1024.0,
string_mem.saturating_sub(arc_mem) as f64 / string_mem as f64 * 100.0
);
println!();
// Test 3: HashMap key performance
println!("--- HashMap Key Performance (100,000 lookups) ---");
let iterations = 100_000;
let mut string_map: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
let mut arc_map: std::collections::HashMap<Arc<str>, usize> = std::collections::HashMap::new();
for i in 0..100 {
string_map.insert(format!("key-{}", i), i);
arc_map.insert(Arc::from(format!("key-{}", i)), i);
}
let start = Instant::now();
for _ in 0..iterations {
for i in 0..100 {
let key = format!("key-{}", i);
let _ = string_map.get(&key);
}
}
let string_lookup_time = start.elapsed();
let start = Instant::now();
for _ in 0..iterations {
for i in 0..100 {
let key: Arc<str> = Arc::from(format!("key-{}", i));
let _ = arc_map.get(&key);
}
}
let arc_lookup_time = start.elapsed();
println!("String key lookup time: {:?}", string_lookup_time);
println!("Arc<str> key lookup time: {:?}", arc_lookup_time);
println!();
// Summary
println!("=== Summary ===");
println!("Arc<str> optimization provides:");
println!(
"1. **Clone speedup: {:.1}x faster** (O(1) refcount vs O(n) string copy)",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
println!(
"2. **Memory savings: {:.1}%** when strings are shared across instances",
string_mem.saturating_sub(arc_mem) as f64 / string_mem as f64 * 100.0
);
println!("3. Better cache locality for frequently accessed shared strings");
println!();
println!("Key insight: The main benefit is clone performance, not raw memory.");
println!("When SubagentInfo is cloned (e.g., for rendering, dashboard updates),");
println!("Arc<str> cloning is ~10x faster than String cloning.");
}
@@ -0,0 +1,322 @@
//! Memory benchmark using real session data.
//!
//! This benchmark loads a real session's updates.jsonl, parses SubagentSpawned events,
//! and measures the memory usage of SubagentInfo with Arc<str> vs String.
//!
//! Run with: cargo run --release --example real_session_benchmark
// Illustrative mock structs whose fields exist to model memory layout; not all
// are read back, which is expected for a microbenchmark.
#![allow(dead_code)]
use std::sync::Arc;
use std::time::Instant;
/// Simulate SubagentInfo with String fields (before optimization)
#[derive(Clone, Debug)]
struct SubagentInfoString {
subagent_id: String,
child_session_id: String,
description: String,
subagent_type: String,
persona: Option<String>,
role: Option<String>,
model: Option<String>,
status: Option<String>,
tools_used: Vec<String>,
}
/// Simulate SubagentInfo with Arc<str> fields (after optimization)
#[derive(Clone, Debug)]
struct SubagentInfoArc {
subagent_id: Arc<str>,
child_session_id: Arc<str>,
description: Arc<str>,
subagent_type: Arc<str>,
persona: Option<Arc<str>>,
role: Option<Arc<str>>,
model: Option<Arc<str>>,
status: Option<Arc<str>>,
tools_used: Vec<Arc<str>>,
}
/// Parsed SubagentSpawned event from updates.jsonl
#[derive(Debug)]
struct SubagentSpawnedEvent {
subagent_id: String,
child_session_id: String,
description: String,
subagent_type: String,
persona: Option<String>,
role: Option<String>,
model: Option<String>,
}
fn parse_subagent_spawned(line: &str) -> Option<SubagentSpawnedEvent> {
// Parse JSON line looking for SubagentSpawned events
let value: serde_json::Value = serde_json::from_str(line).ok()?;
// Check if this is a SubagentSpawned event
// Format: {"params": {"update": {"sessionUpdate": "subagent_spawned", ...}}}
let update = value.get("params")?.get("update")?;
if update.get("sessionUpdate")?.as_str()? != "subagent_spawned" {
return None;
}
Some(SubagentSpawnedEvent {
subagent_id: update.get("subagent_id")?.as_str()?.to_string(),
child_session_id: update.get("child_session_id")?.as_str()?.to_string(),
description: update.get("description")?.as_str()?.to_string(),
subagent_type: update.get("subagent_type")?.as_str()?.to_string(),
persona: update
.get("persona")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
role: update
.get("role")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
model: update
.get("model")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
})
}
fn create_string_info(event: &SubagentSpawnedEvent) -> SubagentInfoString {
SubagentInfoString {
subagent_id: event.subagent_id.clone(),
child_session_id: event.child_session_id.clone(),
description: event.description.clone(),
subagent_type: event.subagent_type.clone(),
persona: event.persona.clone(),
role: event.role.clone(),
model: event.model.clone(),
status: Some("completed".to_string()),
tools_used: vec!["read".to_string(), "search".to_string()],
}
}
fn create_arc_info(event: &SubagentSpawnedEvent) -> SubagentInfoArc {
SubagentInfoArc {
subagent_id: Arc::from(event.subagent_id.as_str()),
child_session_id: Arc::from(event.child_session_id.as_str()),
description: Arc::from(event.description.as_str()),
subagent_type: Arc::from(event.subagent_type.as_str()),
persona: event.persona.as_ref().map(|s| Arc::from(s.as_str())),
role: event.role.as_ref().map(|s| Arc::from(s.as_str())),
model: event.model.as_ref().map(|s| Arc::from(s.as_str())),
status: Some(Arc::from("completed")),
tools_used: vec![Arc::from("read"), Arc::from("search")],
}
}
fn estimate_string_size(info: &SubagentInfoString) -> usize {
info.subagent_id.capacity()
+ info.child_session_id.capacity()
+ info.description.capacity()
+ info.subagent_type.capacity()
+ info.persona.as_ref().map_or(0, |s| s.capacity())
+ info.role.as_ref().map_or(0, |s| s.capacity())
+ info.model.as_ref().map_or(0, |s| s.capacity())
+ info.status.as_ref().map_or(0, |s| s.capacity())
+ info.tools_used.iter().map(|s| s.capacity()).sum::<usize>()
}
fn estimate_arc_size(info: &SubagentInfoArc) -> usize {
// Arc<str> overhead is 16 bytes (fat pointer) per field
// But shared strings are stored once
16 * 4 + // subagent_id, child_session_id, description, subagent_type
info.persona.as_ref().map_or(0, |_| 16) +
info.role.as_ref().map_or(0, |_| 16) +
info.model.as_ref().map_or(0, |_| 16) +
info.status.as_ref().map_or(0, |_| 16) +
info.tools_used.len() * 16 +
// Add actual string lengths (shared, so counted once per unique string)
info.subagent_id.len() +
info.child_session_id.len() +
info.description.len()
}
fn main() {
println!("=== Real Session Memory Benchmark ===\n");
// Require an explicit path — do not hardcode a real session location.
let updates_path = match std::env::var("KIGI_SESSION_PATH") {
Ok(p) => std::path::PathBuf::from(p),
Err(_) => {
eprintln!("Set KIGI_SESSION_PATH to a session's updates.jsonl path");
eprintln!(
"Example: KIGI_SESSION_PATH=$HOME/.kigi/sessions/<cwd-encoded>/019e0000-0000-7000-8000-000000000001/updates.jsonl"
);
return;
}
};
if !updates_path.exists() {
eprintln!("Session not found: {:?}", updates_path);
eprintln!("Set KIGI_SESSION_PATH env var to point to a session's updates.jsonl");
return;
}
let file_size = std::fs::metadata(&updates_path)
.map(|m| m.len())
.unwrap_or(0);
println!("Largest session: {:?}", updates_path);
println!("File size: {:.1} MB", file_size as f64 / 1_000_000.0);
println!();
// Parse SubagentSpawned events
println!("--- Parsing SubagentSpawned events ---");
let start = Instant::now();
let content = std::fs::read_to_string(&updates_path).expect("Failed to read updates.jsonl");
let mut events: Vec<SubagentSpawnedEvent> = Vec::new();
let mut line_count = 0;
for line in content.lines() {
line_count += 1;
if let Some(event) = parse_subagent_spawned(line) {
events.push(event);
}
}
let parse_time = start.elapsed();
println!("Parsed {} lines in {:?}", line_count, parse_time);
println!("Found {} SubagentSpawned events", events.len());
println!();
if events.is_empty() {
println!("No SubagentSpawned events found in this session.");
println!("Trying to find any session with subagents...");
// Try a different approach - look for any session with subagents
return;
}
// Show sample events
println!("--- Sample SubagentSpawned events ---");
for (i, event) in events.iter().take(5).enumerate() {
println!(
"{}. subagent_id={}, type={}, description={:.50}...",
i + 1,
event.subagent_id,
event.subagent_type,
event.description
);
}
println!();
// Create SubagentInfo instances
println!("--- Creating SubagentInfo instances ---");
let string_infos: Vec<SubagentInfoString> = events.iter().map(create_string_info).collect();
let arc_infos: Vec<SubagentInfoArc> = events.iter().map(create_arc_info).collect();
// Calculate memory
let string_mem: usize = string_infos.iter().map(estimate_string_size).sum();
let arc_mem: usize = arc_infos.iter().map(estimate_arc_size).sum();
println!(
"String-based memory: {} bytes ({:.1} KB)",
string_mem,
string_mem as f64 / 1024.0
);
println!(
"Arc<str>-based memory: {} bytes ({:.1} KB)",
arc_mem,
arc_mem as f64 / 1024.0
);
if string_mem > arc_mem {
println!(
"Savings: {} bytes ({:.1} KB, {:.1}%)",
string_mem - arc_mem,
(string_mem - arc_mem) as f64 / 1024.0,
(string_mem - arc_mem) as f64 / string_mem as f64 * 100.0
);
} else {
println!("Note: Arc<str> has overhead for small numbers of instances");
}
println!();
// Analyze string sharing
println!("--- String Sharing Analysis ---");
let mut unique_types = std::collections::HashSet::new();
let mut unique_models = std::collections::HashSet::new();
let mut unique_personas = std::collections::HashSet::new();
for event in &events {
unique_types.insert(&event.subagent_type);
if let Some(ref m) = event.model {
unique_models.insert(m);
}
if let Some(ref p) = event.persona {
unique_personas.insert(p);
}
}
println!(
"Unique subagent_types: {} (from {} events)",
unique_types.len(),
events.len()
);
println!(" Types: {:?}", unique_types);
println!("Unique models: {}", unique_models.len());
println!(" Models: {:?}", unique_models);
println!("Unique personas: {}", unique_personas.len());
println!(" Personas: {:?}", unique_personas);
println!();
// Clone performance
println!("--- Clone Performance (100,000 clones) ---");
let iterations = 100_000;
if let Some(string_info) = string_infos.first() {
let start = Instant::now();
for _ in 0..iterations {
let _ = string_info.clone();
}
let string_clone_time = start.elapsed();
if let Some(arc_info) = arc_infos.first() {
let start = Instant::now();
for _ in 0..iterations {
let _ = arc_info.clone();
}
let arc_clone_time = start.elapsed();
println!("String clone time: {:?}", string_clone_time);
println!("Arc<str> clone time: {:?}", arc_clone_time);
println!(
"Speedup: {:.1}x",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
}
}
println!();
// Summary
println!("=== Summary ===");
println!(
"Session: {:?}",
updates_path.file_name().unwrap_or_default()
);
println!("SubagentSpawned events: {}", events.len());
println!("String sharing potential:");
println!(
" - subagent_type: {} unique values for {} instances",
unique_types.len(),
events.len()
);
println!(" - model: {} unique values", unique_models.len());
println!(" - persona: {} unique values", unique_personas.len());
println!();
println!("Key benefits of Arc<str>:");
println!("1. Clone speedup: ~10x faster (O(1) refcount vs O(n) string copy)");
println!("2. Memory savings when strings are shared across instances");
println!("3. Better cache locality for frequently accessed shared strings");
}