chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record. Tautology/stale/excuse comments are deleted (excuses filed as issues #196-#236), decisions moved to docs/adr/0001-0034. - scripts/lint-comments.sh + .comment-baseline: per-file comment count may only go down; wired into tests/all.sh - .claude/skills/delete-comment: repeatable audit workflow - cargo fmt applied to touched crates Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0545702fe0
commit
a94632ea3d
131 files changed
+1051
-1746
No files matched your search
@@ -1,6 +1,3 @@
|
||||
//! Append-only JSONL session tree. Each entry has `id` + `parent`; the head is the last appended id.
|
||||
//! Forking = appending with an older parent. Resume = walk parents from the head.
|
||||
|
||||
use hx_ai::Message;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{BufRead, Write};
|
||||
@@ -11,14 +8,10 @@ pub struct Entry {
|
||||
pub id: u64,
|
||||
pub parent: Option<u64>,
|
||||
pub message: Message,
|
||||
/// Model that produced an assistant message (absent in older files).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub model: Option<String>,
|
||||
/// API usage of the request that produced this assistant message (absent in older files).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub usage: Option<hx_ai::Usage>,
|
||||
/// Tool-result masking boundary (assistant turns) of the request that produced this assistant
|
||||
/// message; absent in older files, where the stepwise rule applied.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub mask: Option<usize>,
|
||||
}
|
||||
@@ -30,7 +23,6 @@ pub struct Session {
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Open (or create) a session file; head = last entry.
|
||||
pub fn open(path: impl AsRef<Path>) -> std::io::Result<Self> {
|
||||
let path = path.as_ref().to_path_buf();
|
||||
if let Some(d) = path.parent() {
|
||||
@@ -40,7 +32,6 @@ impl Session {
|
||||
if let Ok(f) = std::fs::File::open(&path) {
|
||||
for l in std::io::BufReader::new(f).lines() {
|
||||
let l = l?;
|
||||
// skip a torn last line from a crash rather than losing the session
|
||||
if let Ok(e) = serde_json::from_str::<Entry>(&l) {
|
||||
entries.push(e);
|
||||
}
|
||||
@@ -65,12 +56,10 @@ impl Session {
|
||||
self.append_by(message, None)
|
||||
}
|
||||
|
||||
/// Append, recording which model produced it.
|
||||
pub fn append_by(&mut self, message: Message, model: Option<String>) -> std::io::Result<u64> {
|
||||
self.append_full(message, model, None)
|
||||
}
|
||||
|
||||
/// Append an assistant message with the model and request usage that produced it.
|
||||
pub fn append_full(
|
||||
&mut self,
|
||||
message: Message,
|
||||
@@ -80,7 +69,6 @@ impl Session {
|
||||
self.append_masked(message, model, usage, None)
|
||||
}
|
||||
|
||||
/// Like `append_full`, also recording the masking boundary the request used.
|
||||
pub fn append_masked(
|
||||
&mut self,
|
||||
message: Message,
|
||||
@@ -111,7 +99,6 @@ impl Session {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// Start a new root with `msgs` (used by compaction). Old entries stay in the file.
|
||||
pub fn rebase(&mut self, msgs: Vec<Message>) -> std::io::Result<()> {
|
||||
self.head = None;
|
||||
for m in msgs {
|
||||
@@ -120,7 +107,6 @@ impl Session {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Move head to `id` so the next append branches from it.
|
||||
pub fn fork(&mut self, id: u64) -> bool {
|
||||
let ok = self.entries.iter().any(|e| e.id == id);
|
||||
if ok {
|
||||
@@ -129,7 +115,6 @@ impl Session {
|
||||
ok
|
||||
}
|
||||
|
||||
/// Per-request usage rows (entry id, tool results bytes in the request's input, usage), in file order.
|
||||
pub fn usages(&self) -> Vec<(u64, &hx_ai::Usage)> {
|
||||
self.entries
|
||||
.iter()
|
||||
@@ -137,7 +122,6 @@ impl Session {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Model and usage of every request, in file order (entries from before models were logged have none).
|
||||
pub fn model_usages(&self) -> Vec<(Option<&str>, &hx_ai::Usage)> {
|
||||
self.entries
|
||||
.iter()
|
||||
@@ -159,7 +143,6 @@ impl Session {
|
||||
out
|
||||
}
|
||||
|
||||
/// Messages on the path root..=head.
|
||||
pub fn messages(&self) -> Vec<Message> {
|
||||
self.branch()
|
||||
.into_iter()
|
||||
@@ -167,13 +150,11 @@ impl Session {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Recorded masking boundary per message on the path (aligned with `messages()`).
|
||||
pub fn masks(&self) -> Vec<Option<usize>> {
|
||||
self.branch().into_iter().map(|e| e.mask).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// `<root>/<slug of cwd>`, with `<host>-` in front when `host` is set (shared roots).
|
||||
pub fn dir(root: &Path, cwd: &Path, host: Option<&str>) -> PathBuf {
|
||||
let cwd = cwd.to_string_lossy();
|
||||
let slug: String = host
|
||||
@@ -187,7 +168,6 @@ pub fn dir(root: &Path, cwd: &Path, host: Option<&str>) -> PathBuf {
|
||||
root.join(slug.trim_matches('-'))
|
||||
}
|
||||
|
||||
/// A fresh, not yet created session file in `dir` (created on first append).
|
||||
pub fn new_path(dir: &Path) -> PathBuf {
|
||||
let ts = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
@@ -205,22 +185,16 @@ pub fn new_path(dir: &Path) -> PathBuf {
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Meta {
|
||||
pub path: PathBuf,
|
||||
/// First line of the first user prompt; "(untitled)" when there is none.
|
||||
pub title: String,
|
||||
pub modified: std::time::SystemTime,
|
||||
/// Messages on the head path.
|
||||
pub messages: usize,
|
||||
/// Model of the last assistant entry that recorded one.
|
||||
pub model: Option<String>,
|
||||
/// Leaf entries: >1 means forks or compactions.
|
||||
pub branches: usize,
|
||||
/// Session dir name (project slug).
|
||||
pub project: String,
|
||||
}
|
||||
|
||||
pub const UNTITLED: &str = "(untitled)";
|
||||
|
||||
/// Title of a conversation: first line of the first user text, ≤80 chars.
|
||||
pub fn title(msgs: &[Message]) -> Option<String> {
|
||||
msgs.iter()
|
||||
.filter(|m| m.role == hx_ai::Role::User)
|
||||
@@ -265,8 +239,6 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sessions in `dir`, newest first. Empty files are skipped.
|
||||
// ponytail: parses every file on each call; fine for hundreds of sessions, add an index beyond that.
|
||||
pub fn list(dir: &Path) -> Vec<Meta> {
|
||||
let mut v: Vec<Meta> = std::fs::read_dir(dir)
|
||||
.into_iter()
|
||||
@@ -278,12 +250,10 @@ pub fn list(dir: &Path) -> Vec<Meta> {
|
||||
.filter(|s| !s.entries.is_empty())
|
||||
.map(|s| s.meta())
|
||||
.collect();
|
||||
// equal mtimes (coarse clocks): newer timestamped file name wins
|
||||
v.sort_by(|a, b| (b.modified, &b.path).cmp(&(a.modified, &a.path)));
|
||||
v
|
||||
}
|
||||
|
||||
/// Sessions of every project under the sessions root, newest first.
|
||||
pub fn list_all(root: &Path) -> Vec<Meta> {
|
||||
let mut v: Vec<Meta> = std::fs::read_dir(root)
|
||||
.into_iter()
|
||||
@@ -292,7 +262,6 @@ pub fn list_all(root: &Path) -> Vec<Meta> {
|
||||
.filter(|e| e.path().is_dir())
|
||||
.flat_map(|e| list(&e.path()))
|
||||
.collect();
|
||||
// equal mtimes (coarse clocks): newer timestamped file name wins
|
||||
v.sort_by(|a, b| (b.modified, &b.path).cmp(&(a.modified, &a.path)));
|
||||
v
|
||||
}
|
||||
@@ -328,7 +297,6 @@ mod tests {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let proj = d.path().join("sessions/proj");
|
||||
let mut a = Session::open(proj.join("1.jsonl")).unwrap();
|
||||
// first user message is only a tool result: title comes from the next prompt
|
||||
a.append(Message {
|
||||
role: hx_ai::Role::User,
|
||||
content: vec![hx_ai::ContentBlock::ToolResult {
|
||||
@@ -383,7 +351,6 @@ mod tests {
|
||||
);
|
||||
let n = new_path(&proj);
|
||||
assert!(n.starts_with(&proj) && !n.exists());
|
||||
// old files without `model` still load
|
||||
assert!(
|
||||
serde_json::from_str::<Entry>(
|
||||
r#"{"id":0,"parent":null,"message":{"role":"user","content":[]}}"#
|
||||
|
||||
Reference in new issue
Block a user