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:
@@ -0,0 +1,272 @@
|
||||
//! Inter-compaction chunked pipeline (shared core).
|
||||
//!
|
||||
//! Single pipeline shared by both `CompactionStrategy::Basic` and
|
||||
//! `CompactionStrategy::DivideAndConquer`. The only difference between
|
||||
//! the two is the per-chunk token budget:
|
||||
//!
|
||||
//! - **Basic** → unbounded chunk budget → exactly one chunk.
|
||||
//! - **DivideAndConquer** → `config.dnc_chunk_token_limit` → N chunks.
|
||||
//!
|
||||
//! Everything else — turn filtering, prior-compaction user-query
|
||||
//! extraction, chunk summarisation, and the final `<grok_user_queries>`
|
||||
//! + `<chunk_summary>` assembly — is shared. The harness supplies the
|
||||
//! candidate items, the *current* user-queries preamble (Grok chat
|
||||
//! extracts it from the raw `ChatCompletionRequest`), the sampler, the
|
||||
//! token counter, and an observer for metrics.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use tracing::info;
|
||||
|
||||
use crate::history::filter::{
|
||||
assemble_user_queries_preamble, filter_turns_for_inter_compaction, separate_prior_user_queries,
|
||||
wrap_chunk_analysis,
|
||||
};
|
||||
use crate::history::prompt::{format_compaction_developer_prompt, format_compaction_user_prompt};
|
||||
use crate::history::types::CompactionStrategy;
|
||||
use crate::item::CompactionItemBuilder;
|
||||
use crate::prompt::CompactionPrompt;
|
||||
use crate::sampler::{CompactionSampleError, CompactionSampler, LlmCompactionOutput};
|
||||
use crate::token::ItemTokenCounter;
|
||||
|
||||
use super::config::InterCompactionConfig;
|
||||
use super::observer::InterCompactionObserver;
|
||||
|
||||
/// Sentinel chunk budget used by [`CompactionStrategy::Basic`] so the
|
||||
/// chunking loop emits exactly one chunk.
|
||||
const UNBOUNDED_CHUNK_LIMIT: u32 = u32::MAX;
|
||||
|
||||
/// Output of the shared chunked pipeline — assembled text, not yet wrapped
|
||||
/// into a harness message type.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChunkedCompactionOutput {
|
||||
/// `<grok_user_queries>` preamble + `<chunk_summary index="i">` blocks.
|
||||
/// The harness wraps this into its summary-carrier message.
|
||||
pub combined_text: String,
|
||||
/// Thinking-channel output: `<chunk_analysis>` blocks. Empty when the
|
||||
/// model produced no thinking output. Stored for audit/debug only.
|
||||
pub analysis_text: String,
|
||||
}
|
||||
|
||||
/// Shared chunked pipeline.
|
||||
///
|
||||
/// Steps:
|
||||
/// 1. Filter items with
|
||||
/// [`filter_turns_for_inter_compaction`](crate::history::filter::filter_turns_for_inter_compaction).
|
||||
/// 2. [`separate_prior_user_queries`] — split prior `<grok_user_queries>`
|
||||
/// blocks out of every prior compaction summary item. The LLM never sees
|
||||
/// them. Shared with intra-compaction's `History` target so both
|
||||
/// pipelines handle re-compactions identically.
|
||||
/// 3. Walk the LLM-safe item list. Flush a chunk whenever the running
|
||||
/// token count would exceed the chunk budget (`UNBOUNDED_CHUNK_LIMIT`
|
||||
/// for Basic — single chunk).
|
||||
/// 4. Combine `prior_user_queries + current_user_queries + <chunk_summary>`
|
||||
/// blocks into the final summary text via
|
||||
/// [`assemble_user_queries_preamble`]; combine the per-chunk
|
||||
/// `thinking` channels into the analysis text.
|
||||
///
|
||||
/// `current_user_queries` is the harness-extracted preamble for *this*
|
||||
/// round's user messages (Grok chat: verbatim from the raw request, with
|
||||
/// attachment refs). `conversation_id` / `response_id` are threaded
|
||||
/// through for log correlation only.
|
||||
///
|
||||
/// Observer events (the Grok chat observer maps them to the
|
||||
/// pre-unification metrics):
|
||||
/// - [`InterCompactionObserver::on_recompaction`] when prior-compaction
|
||||
/// summary items are found.
|
||||
/// - [`InterCompactionObserver::on_chunk_count`] — chunk count after
|
||||
/// assembly (always 1 for Basic; N for DnC).
|
||||
/// - [`InterCompactionObserver::on_chunk_sampled`] — per-chunk LLM latency.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn sample_compaction_chunked<T: CompactionItemBuilder + Send + Sync>(
|
||||
turns: &[T],
|
||||
current_user_queries: Option<String>,
|
||||
conversation_id: &str,
|
||||
response_id: &str,
|
||||
start_response_id: &str,
|
||||
config: &InterCompactionConfig,
|
||||
sampler: &dyn CompactionSampler<Item = T>,
|
||||
token_counter: &dyn ItemTokenCounter<T>,
|
||||
observer: &dyn InterCompactionObserver,
|
||||
) -> Result<ChunkedCompactionOutput, CompactionSampleError> {
|
||||
let chunk_token_limit = match config.compaction_strategy {
|
||||
CompactionStrategy::Basic => UNBOUNDED_CHUNK_LIMIT,
|
||||
CompactionStrategy::DivideAndConquer => config.dnc_chunk_token_limit,
|
||||
CompactionStrategy::FullReplace => {
|
||||
return Err(CompactionSampleError::Build(
|
||||
"full_replace must be routed through the event-proc compact_conversation helper"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
let strategy_label = config.compaction_strategy.label();
|
||||
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
response_id = %response_id,
|
||||
strategy = strategy_label,
|
||||
num_turns = turns.len(),
|
||||
chunk_token_limit,
|
||||
user_compact_threshold = config.user_message_compact_threshold,
|
||||
"[InterCompaction] starting chunked compaction"
|
||||
);
|
||||
|
||||
// Step 1 — filter.
|
||||
let filtered = filter_turns_for_inter_compaction(turns);
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
start_response_id = %start_response_id,
|
||||
last_response_id = %response_id,
|
||||
original = turns.len(),
|
||||
filtered = filtered.len(),
|
||||
"[InterCompaction] filtered turns"
|
||||
);
|
||||
if filtered.is_empty() {
|
||||
return Err(CompactionSampleError::Other(anyhow::anyhow!(
|
||||
"No turns remaining after filtering"
|
||||
)));
|
||||
}
|
||||
|
||||
// Step 2 — split prior `<grok_user_queries>` out of every prior
|
||||
// compaction summary item. The LLM never sees them (it would re-emit
|
||||
// them verbatim and snowball across rounds); they are reattached to
|
||||
// the final summary via `assemble_user_queries_preamble`. Shared with
|
||||
// intra-compaction's `History` target.
|
||||
let separated = separate_prior_user_queries(&filtered);
|
||||
|
||||
// Step 3 — chunk + flush over the LLM-safe item list.
|
||||
let mut compactable: Vec<T> = Vec::new();
|
||||
let mut chunk_tokens: u32 = 0;
|
||||
let mut chunk_outputs: Vec<LlmCompactionOutput> = Vec::new();
|
||||
let mut chunk_idx: usize = 0;
|
||||
|
||||
for turn in &separated.turns_for_llm {
|
||||
let turn_tokens = token_counter.count_item_tokens(turn);
|
||||
// Flush the current chunk if adding this item would exceed the
|
||||
// budget (`UNBOUNDED_CHUNK_LIMIT` disables flushing — Basic).
|
||||
if !compactable.is_empty()
|
||||
&& chunk_token_limit != UNBOUNDED_CHUNK_LIMIT
|
||||
&& chunk_tokens.saturating_add(turn_tokens) > chunk_token_limit
|
||||
{
|
||||
let output = flush_chunk(
|
||||
&compactable,
|
||||
conversation_id,
|
||||
response_id,
|
||||
chunk_idx,
|
||||
config,
|
||||
sampler,
|
||||
token_counter,
|
||||
observer,
|
||||
)
|
||||
.await?;
|
||||
chunk_outputs.push(output);
|
||||
chunk_idx += 1;
|
||||
compactable.clear();
|
||||
chunk_tokens = 0;
|
||||
}
|
||||
compactable.push(turn.clone());
|
||||
chunk_tokens += turn_tokens;
|
||||
}
|
||||
|
||||
// Final flush — one chunk for Basic, the trailing chunk for DnC.
|
||||
if !compactable.is_empty() {
|
||||
let output = flush_chunk(
|
||||
&compactable,
|
||||
conversation_id,
|
||||
response_id,
|
||||
chunk_idx,
|
||||
config,
|
||||
sampler,
|
||||
token_counter,
|
||||
observer,
|
||||
)
|
||||
.await?;
|
||||
chunk_outputs.push(output);
|
||||
}
|
||||
|
||||
if separated.has_prior_compaction {
|
||||
observer.on_recompaction(strategy_label);
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
strategy = strategy_label,
|
||||
"[InterCompaction] Re-compaction detected"
|
||||
);
|
||||
}
|
||||
|
||||
// Step 4a — combine summaries.
|
||||
let preamble =
|
||||
assemble_user_queries_preamble(separated.prior_user_queries, current_user_queries);
|
||||
let mut combined = preamble;
|
||||
for (i, output) in chunk_outputs.iter().enumerate() {
|
||||
combined.push_str(&format!("<chunk_summary index=\"{}\">\n", i));
|
||||
combined.push_str(&output.response);
|
||||
combined.push_str("\n</chunk_summary>\n\n");
|
||||
}
|
||||
|
||||
// Step 4b — combine thinking-channel output.
|
||||
let mut combined_analysis = String::new();
|
||||
for (i, output) in chunk_outputs.iter().enumerate() {
|
||||
combined_analysis.push_str(&wrap_chunk_analysis(i, &output.thinking));
|
||||
}
|
||||
|
||||
// Record chunk count after assembly so dashboards see the same timing
|
||||
// they saw pre-unification (where this lived inside DnC).
|
||||
observer.on_chunk_count(chunk_outputs.len());
|
||||
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
response_id = %response_id,
|
||||
strategy = strategy_label,
|
||||
num_chunks = chunk_outputs.len(),
|
||||
combined_len = combined.len(),
|
||||
analysis_len = combined_analysis.len(),
|
||||
"[InterCompaction] chunked compaction complete"
|
||||
);
|
||||
|
||||
Ok(ChunkedCompactionOutput {
|
||||
combined_text: combined,
|
||||
analysis_text: combined_analysis,
|
||||
})
|
||||
}
|
||||
|
||||
/// Compact a single chunk of items via the LLM.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn flush_chunk<T: CompactionItemBuilder + Send + Sync>(
|
||||
turns: &[T],
|
||||
conversation_id: &str,
|
||||
response_id: &str,
|
||||
chunk_idx: usize,
|
||||
config: &InterCompactionConfig,
|
||||
sampler: &dyn CompactionSampler<Item = T>,
|
||||
token_counter: &dyn ItemTokenCounter<T>,
|
||||
observer: &dyn InterCompactionObserver,
|
||||
) -> Result<LlmCompactionOutput, CompactionSampleError> {
|
||||
let total_tokens: u32 = turns
|
||||
.iter()
|
||||
.map(|t| token_counter.count_item_tokens(t))
|
||||
.sum();
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
response_id = %response_id,
|
||||
chunk_idx = chunk_idx,
|
||||
num_turns = turns.len(),
|
||||
total_tokens = total_tokens,
|
||||
"[InterCompaction] Compacting chunk"
|
||||
);
|
||||
let prompt = CompactionPrompt {
|
||||
system: format_compaction_developer_prompt().map_err(CompactionSampleError::from)?,
|
||||
user: format_compaction_user_prompt().map_err(CompactionSampleError::from)?,
|
||||
};
|
||||
let timeout = Duration::from_secs(config.sampling_timeout_secs);
|
||||
let t0 = Instant::now();
|
||||
let result = sampler.sample_compaction(turns, &prompt, timeout).await;
|
||||
observer.on_chunk_sampled(result.is_ok(), t0.elapsed());
|
||||
info!(
|
||||
conversation_id = %conversation_id,
|
||||
chunk_idx = chunk_idx,
|
||||
elapsed_ms = t0.elapsed().as_millis() as u64,
|
||||
success = result.is_ok(),
|
||||
"[InterCompaction] Chunk compaction done"
|
||||
);
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//! Configuration for inter-compaction.
|
||||
//!
|
||||
//! This is a plain data struct — harness-specific service-config integration
|
||||
//! stays in the compaction subscriber, which resolves config values and
|
||||
//! constructs this struct.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::history::types::CompactionStrategy;
|
||||
|
||||
/// Runtime configuration for a single inter-compaction invocation.
|
||||
///
|
||||
/// Mirrors the fields used by the between-turn compaction service config,
|
||||
/// without a harness-specific config-macro dependency.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InterCompactionConfig {
|
||||
/// The agent/scheduler name to use for the compaction model.
|
||||
///
|
||||
/// NOTE: model routing is host policy — kept here only because
|
||||
/// service configs deserialize this struct as-is; slated to move to the
|
||||
/// per-harness policy split in a later phase.
|
||||
pub compaction_model_name: String,
|
||||
/// End-to-end timeout for the compaction sampling in seconds.
|
||||
pub sampling_timeout_secs: u64,
|
||||
/// Which compaction strategy to use.
|
||||
pub compaction_strategy: CompactionStrategy,
|
||||
/// [DivideAndConquer] Max tokens per chunk before sending to the LLM.
|
||||
/// (Basic strategy ignores this and emits a single chunk.)
|
||||
pub dnc_chunk_token_limit: u32,
|
||||
/// User messages with character count > this threshold are truncated
|
||||
/// (middle-cut) when assembling the `<grok_user_queries>` preamble.
|
||||
/// Applies to both Basic and DivideAndConquer.
|
||||
pub user_message_compact_threshold: u32,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//! Inter-compaction — the chunked summarisation pipeline shared by both
|
||||
//! `Basic` and `DivideAndConquer` strategies, generic over
|
||||
//! [`CompactionItemBuilder`](crate::CompactionItemBuilder).
|
||||
//!
|
||||
//! Harness wiring (turn selection from the conversation store, raw-request
|
||||
//! user-query extraction, summary-message assembly, persistence) stays
|
||||
//! per-harness; the Grok chat host wraps this pipeline.
|
||||
|
||||
pub mod compact;
|
||||
pub mod config;
|
||||
pub mod observer;
|
||||
|
||||
pub use compact::{ChunkedCompactionOutput, sample_compaction_chunked};
|
||||
pub use config::InterCompactionConfig;
|
||||
pub use observer::InterCompactionObserver;
|
||||
@@ -0,0 +1,23 @@
|
||||
//! Observability seam for inter-compaction.
|
||||
//!
|
||||
//! Same rationale as [`crate::intra_compaction::observer`]: the shared
|
||||
//! pipeline reports events; each harness emits its own metrics. Emission
|
||||
//! points and label values are part of the behavior contract.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
/// Receives inter-compaction pipeline events. All methods default to no-ops.
|
||||
pub trait InterCompactionObserver: Send + Sync {
|
||||
/// A prior compaction summary was found in the input (re-compaction).
|
||||
/// `strategy` is the stable label from `CompactionStrategy::label()`.
|
||||
fn on_recompaction(&self, _strategy: &'static str) {}
|
||||
|
||||
/// One chunk's LLM call finished (success or error).
|
||||
fn on_chunk_sampled(&self, _success: bool, _elapsed: Duration) {}
|
||||
|
||||
/// The whole pipeline finished assembling `num_chunks` chunk summaries.
|
||||
fn on_chunk_count(&self, _num_chunks: usize) {}
|
||||
}
|
||||
|
||||
/// No-op observer for tests and harnesses without metrics.
|
||||
impl InterCompactionObserver for () {}
|
||||
Reference in New Issue
Block a user