Repository navigation
feat(agent-sessions): add the local agent session record store - #199
Merged
Merged
Conversation
Add lithe-agent-sessions, a pure crate that keeps a SQLite cache of the call records agent plugins parse from session files on disk. Core holds no agent names, directories or formats: a plugin backend lists the files to read and turns each chunk of complete lines into session, call and index records; Core reads, validates, stores, counts and searches them. - discover: list the files a plugin's root and glob patterns match, without following symbolic links, with bounded counts. - ingest/store: read appended lines from a cursor, detect rewrites and truncation by size and a fingerprint of the first and last bytes read, skip overlong lines, retry a failed chunk, and forget files that are unlisted or deleted. - store: query calls with copies of a resumed session counted once, list sessions merged across their files, and read back a byte range only while the file is unchanged. - text: index CJK and other unspaced scripts as character pairs and words as prefixes in a contentless FTS5 table, so the cache keeps no conversation text; query syntax never reaches FTS5. - stats/pricing: sum tokens, cache hit rate and failures by agent, model, session, directory or local day; price calls from a models.dev table as an API-price equivalent; rank models by median output speed. The plugin API (API-45), MCP tools and log events come in a later change.
xiaoyumuxi
marked this pull request as ready for review
November 25, 2026 07:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Agent 原生调用官方 CLI 设计(1lck/Lithe-IDEA#2407,API-45)的第一阶段:新增纯 crate
lithe-agent-sessions,在本地 SQLite 缓存里保存 Agent 会话的调用记录,用于调用统计、token 统计、费用估算和会话搜索。Core 不含任何 Agent 名称、目录或格式。插件后端给出要读的根目录和 glob 规则,并把每一块完整行解析成会话、调用和索引记录;Core 负责读文件、校验、入库、统计和搜索。缓存里不存对话正文:搜索索引是 contentless FTS5,结果只返回字节范围,需要展示时再从原文件读回,文件改动后不再读回。
这个 PR 只有库本身,不改插件契约,不新增命令、MCP 工具或日志事件;这些放在第二阶段。
Changes
rust/crates/agent-sessions/src/discover.rs:按插件给的根目录和 glob 列出会话文件,不跟随符号链接,每个来源最多 100000 个文件,超出时标记truncated;根目录不存在时返回空结果,非法 glob 返回invalid_request。src/ingest.rs、src/store.rs(增量读取):parse_failed,详情里只写字段名,不带值。src/store.rs(查询):(agent, call_key)去重,续接会话复制出来的调用只算一次;cwd 优先取调用所在文件的会话。read_range只在文件未改动时读回字节。user_version不符的数据库会删除重建。src/text.rs:中日韩等不用空格分词的文字按相邻二字索引,其余文字按小写单词前缀匹配;查询文本不会被当成 FTS5 语法执行(引号、OR、NEAR、-、^、列过滤都只当普通词处理)。src/stats.rs:按 agent、模型、会话、目录或用户时区的自然日汇总调用数、失败数、压缩次数、各类 token、缓存命中率和费用;按输出速度中位数给模型排序,没有首 token 时间的调用只算下限。src/pricing.rs:读取 models.dev 格式的价格表(最大 32 MiB),支持缓存读写价、推理价和按上下文长度分档;缓存或推理没有单独价格时按输入价计并标记为近似值。费用口径是“按 API 价格折算”,不代表订阅用户的实际支出。src/tests/:41 个测试,覆盖上面的行为,包括“缓存目录里找不到对话原文”的检查。留给后续阶段或需要评审关注
agents.*、agentSessions.*)、MCP 工具lithe_sessions_search/lithe_sessions_read和rust/logging/events/agent.json日志事件在第二阶段加入,所以这个 PR 不改契约,也不改更新记录。detail='none',没有位置信息,所以多个词只保证都出现在同一段里,不保证相邻。lithe-remote已经在用的 0.32(打包 SQLite 3.46),整个工作区只链接一份 SQLite。Verification
cargo test --manifest-path rust/Cargo.toml -p lithe-agent-sessions→ 41 passedcargo fmt --manifest-path rust/Cargo.toml --all -- --check→ passedcargo clippy --locked --manifest-path rust/Cargo.toml -p lithe-agent-sessions --all-targets --all-features -- -D warnings→ passedLIBSQLITE3_SYS_USE_PKG_CONFIG=1 ZSTD_SYS_USE_PKG_CONFIG=1 PKG_CONFIG_ALLOW_CROSS=1 cargo clippy --locked ... -p lithe-agent-sessions --all-targets --target x86_64-pc-windows-msvc -- -D warnings→ passedscripts/rust-core/verify-rust-core-comments.sh agent-sessions→ passednode scripts/logging/generate-log-events.mjs --check→ passednode scripts/rust-core/verify-platform-tests.mjs→ passed(唯一的#[cfg(unix)]测试是“不跟随符号链接”,在 Linux 和 macOS 上运行)node --test scripts/rust-core/*.test.mjs→ 16 passed