Files
Kigi-CLI/crates/codegen/kigi-shell/src/session/memory_state.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

239 lines
9.4 KiB
Rust

//! `SessionMemory` — memory subsystem state for the session actor.
//!
//! Groups storage, flush config, injection state, and telemetry counters
//! that were previously scattered across 15 fields on `SessionActor`.
use std::cell::RefCell;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64};
/// Memory subsystem state for a session.
pub struct SessionMemory {
/// Memory storage handle for writing flush output (None when memory disabled).
/// Wrapped in `RefCell` to allow `/memory on|off` toggle from `&Arc<SessionActor>`.
pub storage: RefCell<Option<crate::session::memory::MemoryStorage>>,
/// Whether to write a session summary to memory on session end.
pub save_on_end: bool,
/// Shared params for building a fully-configured memory backend.
/// `None` when memory is disabled.
pub backend_params: Option<crate::session::memory::MemoryBackendParams>,
/// First-turn memory injection behavior resolved from local + remote config.
pub initial_injection_config: crate::config::MemoryInitialInjectionConfig,
/// Per-process latch: the first-turn injection decision already ran in
/// this session segment. Cross-segment idempotency comes from
/// `conversation_has_memory_context`, not this flag.
pub context_injected: AtomicBool,
/// Memory flush configuration (from MemoryConfig).
pub flush_config: crate::config::MemoryFlushConfig,
/// When `true`, auto-compact checks are suppressed during memory flush.
pub is_flushing: AtomicBool,
/// The compaction count at which the last flush ran (once-per-cycle guard).
pub last_flush_compaction: AtomicU64,
/// Number of flushes executed in this session.
pub flush_count: AtomicU64,
/// Content from the most recent successful flush, used for delta prompts.
/// Wrapped in `RefCell` because `SessionActor` is single-threaded (LocalSet).
pub last_flush_content: RefCell<Option<String>>,
/// Number of successful flushes.
pub flush_success_count: AtomicU64,
/// Number of failed flushes.
pub flush_error_count: AtomicU64,
/// Counts model-initiated `memory_search` tool calls.
/// Wrapped in `RefCell` to allow `/memory on|off` toggle from `&Arc<SessionActor>`.
pub search_counter: RefCell<Option<Arc<AtomicU64>>>,
/// Counts first-turn memory context injections.
pub injection_count: AtomicU64,
/// Counts post-compaction memory re-injection searches.
pub compaction_recovery_count: AtomicU64,
/// Total memory chunks added across all sources.
pub chunks_added: Arc<AtomicU64>,
/// autoDream consolidation config.
pub dream_config: crate::config::MemoryDreamConfig,
/// Number of dream consolidations attempted.
pub dream_count: AtomicU64,
/// Number of successful dream consolidations.
pub dream_success_count: AtomicU64,
/// Number of failed dream consolidations.
pub dream_error_count: AtomicU64,
}
impl SessionMemory {
/// Whether memory is enabled for this session.
pub fn is_enabled(&self) -> bool {
self.storage.borrow().is_some()
}
/// Clone the storage out of the `RefCell`, dropping the borrow immediately.
pub fn storage(&self) -> Option<crate::session::memory::MemoryStorage> {
self.storage.borrow().clone()
}
/// Attempt to acquire the flush lock. Returns `true` if acquired,
/// `false` if another flush is already in progress.
pub fn try_acquire_flush_lock(&self) -> bool {
self.is_flushing
.compare_exchange(
false,
true,
std::sync::atomic::Ordering::Relaxed,
std::sync::atomic::Ordering::Relaxed,
)
.is_ok()
}
/// Release the flush lock.
pub fn release_flush_lock(&self) {
self.is_flushing
.store(false, std::sync::atomic::Ordering::Relaxed);
}
/// Record a flush result and increment the appropriate counter.
///
/// Matches the original three-way logic: "written" increments success,
/// "error" increments error, anything else ("nothing_to_store", "rejected")
/// increments only the total flush count.
pub fn record_flush_result(&self, outcome: &str) {
self.flush_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
match outcome {
"written" => {
self.flush_success_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
"error" => {
self.flush_error_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
_ => {}
}
}
/// Record a dream consolidation result.
pub fn record_dream_result(&self, success: bool) {
use std::sync::atomic::Ordering::Relaxed;
self.dream_count.fetch_add(1, Relaxed);
if success {
self.dream_success_count.fetch_add(1, Relaxed);
} else {
self.dream_error_count.fetch_add(1, Relaxed);
}
}
/// Record a neutral dream outcome (nothing to consolidate / skipped).
pub fn record_dream_neutral(&self) {
self.dream_count
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
/// Open (or create) the memory index for the current workspace.
///
/// Shared helper that extracts embed dimensions from `backend_params`
/// and opens the index at `<workspace_dir>/index.sqlite`.
pub(crate) fn open_index(
&self,
storage: &crate::session::memory::MemoryStorage,
) -> Option<crate::session::memory::MemoryIndex> {
let embed_dims = self
.backend_params
.as_ref()
.and_then(|p| p.embed_config.as_ref())
.map_or(1024, |c| c.dimensions);
let db_path = storage.workspace_dir().join("index.sqlite");
crate::session::memory::MemoryIndex::open_or_create(
&db_path,
storage.clone(),
Default::default(),
embed_dims,
)
.ok()
}
/// Reindex a file and embed new chunks when embedding is configured.
pub async fn reindex_and_embed(&self, path: &std::path::Path, source: &str) {
let Some(storage) = self.storage.borrow().clone() else {
return;
};
if let Some(mut index) = self.open_index(&storage) {
let _ = index.reindex_file(path, source);
if let Some(ref params) = self.backend_params
&& let Some(provider) = params.make_embedding_provider().await
{
crate::session::memory::embed_missing_chunks(&index, &provider).await;
}
}
}
/// Remove chunks for the given file paths from the search index.
///
/// Used after dream consolidation deletes processed session files so
/// that stale chunks don't linger in the index. Best-effort: errors
/// are logged but don't propagate.
pub fn delete_paths_from_index(&self, paths: &[std::path::PathBuf]) {
if paths.is_empty() {
return;
}
let Some(storage) = self.storage.borrow().clone() else {
return;
};
if let Some(mut index) = self.open_index(&storage) {
let mut total_removed = 0usize;
for path in paths {
match index.delete_path(path) {
Ok(n) => total_removed += n,
Err(e) => {
tracing::warn!(
target: kigi_log::memory_log::TARGET,
path = %path.display(),
error = %e,
"DREAM_CLEANUP: failed to remove chunks from index"
);
}
}
}
if total_removed > 0 {
tracing::info!(
target: kigi_log::memory_log::TARGET,
chunks_removed = total_removed,
files = paths.len(),
"DREAM_CLEANUP: removed stale chunks from index"
);
}
}
}
/// Collect telemetry counters for session-end summary.
pub fn telemetry_snapshot(&self) -> MemoryTelemetry {
use std::sync::atomic::Ordering::Relaxed;
MemoryTelemetry {
flush_count: self.flush_count.load(Relaxed),
flush_success_count: self.flush_success_count.load(Relaxed),
flush_error_count: self.flush_error_count.load(Relaxed),
tool_search_count: self
.search_counter
.borrow()
.as_ref()
.map_or(0, |c| c.load(Relaxed)),
injection_count: self.injection_count.load(Relaxed),
compaction_recovery_count: self.compaction_recovery_count.load(Relaxed),
chunks_added: self.chunks_added.load(Relaxed),
dream_count: self.dream_count.load(Relaxed),
dream_success_count: self.dream_success_count.load(Relaxed),
dream_error_count: self.dream_error_count.load(Relaxed),
}
}
}
/// Snapshot of memory telemetry counters for session-end logging.
pub struct MemoryTelemetry {
pub flush_count: u64,
pub flush_success_count: u64,
pub flush_error_count: u64,
pub tool_search_count: u64,
pub injection_count: u64,
pub compaction_recovery_count: u64,
pub chunks_added: u64,
pub dream_count: u64,
pub dream_success_count: u64,
pub dream_error_count: u64,
}