Skip to content

feat(agent-sessions): add the local agent session record store - #199

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/agent-sessions-store
Nov 25, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/agent-sessions-store

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

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(增量读取):
    • 从游标续读追加的行,按文件大小和已读部分首尾各 4 KiB 的 SHA-256 指纹判断改写和截断,判断为改写或截断时从头重读。
    • 末尾没有换行的行等文件写完再读;超长行跳过并计数。
    • 某一块解析失败时,这一块下次同步会重新交给插件,失败只影响该文件。
    • 不再列出的文件和已删除的文件会清掉对应记录。
    • 插件输出逐项校验:ID 为不含空格的可打印 ASCII、标签长度、cwd 不含控制字符、范围落在本块内、数值上限。校验失败返回 parse_failed,详情里只写字段名,不带值。
  • src/store.rs(查询):
    • 调用记录按 (agent, call_key) 去重,续接会话复制出来的调用只算一次;cwd 优先取调用所在文件的会话。
    • 会话列表合并同一会话的多个文件,按最近活动时间排序,可按 agent、cwd、活跃时间过滤并分页。
    • read_range 只在文件未改动时读回字节。
    • 缓存文件名带 schema 版本;旧版本文件、损坏的数据库或 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 个测试,覆盖上面的行为,包括“缓存目录里找不到对话原文”的检查。

留给后续阶段或需要评审关注

  • 插件 API(API-45:agents.*、agentSessions.*)、MCP 工具 lithe_sessions_search / lithe_sessions_read 和 rust/logging/events/agent.json 日志事件在第二阶段加入,所以这个 PR 不改契约,也不改更新记录。
  • 不记录账号字段:设计上不读取、不保存任何凭据或账号标识。
  • 价格表随包打包和定期刷新还没做,这里只提供解析和计算。
  • FTS 用 detail='none',没有位置信息,所以多个词只保证都出现在同一段里,不保证相邻。
  • 搜索结果没有在续接会话复制出的多个文件之间去重(调用统计已去重)。
  • 只被引用、没有被插件描述过的会话行不带开始时间、更新时间和 cwd。
  • rusqlite 沿用工作区里 lithe-remote 已经在用的 0.32(打包 SQLite 3.46),整个工作区只链接一份 SQLite。

Verification

  • cargo test --manifest-path rust/Cargo.toml -p lithe-agent-sessions → 41 passed
  • cargo fmt --manifest-path rust/Cargo.toml --all -- --check → passed
  • cargo clippy --locked --manifest-path rust/Cargo.toml -p lithe-agent-sessions --all-targets --all-features -- -D warnings → passed
  • LIBSQLITE3_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 → passed
  • scripts/rust-core/verify-rust-core-comments.sh agent-sessions → passed
  • node scripts/logging/generate-log-events.mjs --check → passed
  • node scripts/rust-core/verify-platform-tests.mjs → passed(唯一的 #[cfg(unix)] 测试是“不跟随符号链接”,在 Linux 和 macOS 上运行)
  • node --test scripts/rust-core/*.test.mjs → 16 passed
  • crate 内搜索 Agent 和厂商名称 → 无结果

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
xiaoyumuxi marked this pull request as ready for review November 25, 2026 07:03
@xiaoyumuxi
xiaoyumuxi merged commit 5e69a21 into main Nov 25, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant