mirror of
https://github.com/lWolvesl/claw-code.git
synced 2026-04-02 07:41:52 +08:00
Persist CLI conversation history across sessions
The Rust CLI now stores managed sessions under ~/.claude/sessions, records additive session metadata in the canonical JSON transcript, and exposes a /sessions listing alias alongside ID-or-path resume. Inactive oversized sessions are compacted automatically so old transcripts remain resumable without growing unchecked. Constraint: Session JSON must stay backward-compatible with legacy files that lack metadata Constraint: Managed sessions must use a single canonical JSON file per session without new dependencies Rejected: Sidecar metadata/index files | duplicated state and diverged from the requested single-file persistence model Confidence: high Scope-risk: moderate Reversibility: clean Directive: Keep CLI policy in the CLI; only add transcript-adjacent metadata to runtime::Session unless another consumer truly needs more Tested: cargo fmt; cargo clippy --workspace --all-targets -- -D warnings; cargo test --workspace Not-tested: Manual interactive REPL smoke test against the live Anthropic API
This commit is contained in:
@@ -105,6 +105,7 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
||||
compacted_session: Session {
|
||||
version: session.version,
|
||||
messages: compacted_messages,
|
||||
metadata: session.metadata.clone(),
|
||||
},
|
||||
removed_message_count: removed.len(),
|
||||
}
|
||||
@@ -393,6 +394,7 @@ mod tests {
|
||||
let session = Session {
|
||||
version: 1,
|
||||
messages: vec![ConversationMessage::user_text("hello")],
|
||||
metadata: None,
|
||||
};
|
||||
|
||||
let result = compact_session(&session, CompactionConfig::default());
|
||||
@@ -420,6 +422,7 @@ mod tests {
|
||||
usage: None,
|
||||
},
|
||||
],
|
||||
metadata: None,
|
||||
};
|
||||
|
||||
let result = compact_session(
|
||||
|
||||
@@ -408,7 +408,7 @@ mod tests {
|
||||
.sum::<i32>();
|
||||
Ok(total.to_string())
|
||||
});
|
||||
let permission_policy = PermissionPolicy::new(PermissionMode::Prompt);
|
||||
let permission_policy = PermissionPolicy::new(PermissionMode::WorkspaceWrite);
|
||||
let system_prompt = SystemPromptBuilder::new()
|
||||
.with_project_context(ProjectContext {
|
||||
cwd: PathBuf::from("/tmp/project"),
|
||||
@@ -487,7 +487,7 @@ mod tests {
|
||||
Session::new(),
|
||||
SingleCallApiClient,
|
||||
StaticToolExecutor::new(),
|
||||
PermissionPolicy::new(PermissionMode::Prompt),
|
||||
PermissionPolicy::new(PermissionMode::WorkspaceWrite),
|
||||
vec!["system".to_string()],
|
||||
);
|
||||
|
||||
@@ -536,7 +536,7 @@ mod tests {
|
||||
session,
|
||||
SimpleApi,
|
||||
StaticToolExecutor::new(),
|
||||
PermissionPolicy::new(PermissionMode::Allow),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
);
|
||||
|
||||
@@ -563,7 +563,7 @@ mod tests {
|
||||
Session::new(),
|
||||
SimpleApi,
|
||||
StaticToolExecutor::new(),
|
||||
PermissionPolicy::new(PermissionMode::Allow),
|
||||
PermissionPolicy::new(PermissionMode::DangerFullAccess),
|
||||
vec!["system".to_string()],
|
||||
);
|
||||
runtime.run_turn("a", None).expect("turn a");
|
||||
|
||||
@@ -73,7 +73,9 @@ pub use remote::{
|
||||
RemoteSessionContext, UpstreamProxyBootstrap, UpstreamProxyState, DEFAULT_REMOTE_BASE_URL,
|
||||
DEFAULT_SESSION_TOKEN_PATH, DEFAULT_SYSTEM_CA_BUNDLE, NO_PROXY_HOSTS, UPSTREAM_PROXY_ENV_KEYS,
|
||||
};
|
||||
pub use session::{ContentBlock, ConversationMessage, MessageRole, Session, SessionError};
|
||||
pub use session::{
|
||||
ContentBlock, ConversationMessage, MessageRole, Session, SessionError, SessionMetadata,
|
||||
};
|
||||
pub use usage::{
|
||||
format_usd, pricing_for_model, ModelPricing, TokenUsage, UsageCostEstimate, UsageTracker,
|
||||
};
|
||||
|
||||
@@ -39,10 +39,19 @@ pub struct ConversationMessage {
|
||||
pub usage: Option<TokenUsage>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SessionMetadata {
|
||||
pub started_at: String,
|
||||
pub model: String,
|
||||
pub message_count: u32,
|
||||
pub last_prompt: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Session {
|
||||
pub version: u32,
|
||||
pub messages: Vec<ConversationMessage>,
|
||||
pub metadata: Option<SessionMetadata>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -82,6 +91,7 @@ impl Session {
|
||||
Self {
|
||||
version: 1,
|
||||
messages: Vec::new(),
|
||||
metadata: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +121,9 @@ impl Session {
|
||||
.collect(),
|
||||
),
|
||||
);
|
||||
if let Some(metadata) = &self.metadata {
|
||||
object.insert("metadata".to_string(), metadata.to_json());
|
||||
}
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
@@ -131,7 +144,15 @@ impl Session {
|
||||
.iter()
|
||||
.map(ConversationMessage::from_json)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(Self { version, messages })
|
||||
let metadata = object
|
||||
.get("metadata")
|
||||
.map(SessionMetadata::from_json)
|
||||
.transpose()?;
|
||||
Ok(Self {
|
||||
version,
|
||||
messages,
|
||||
metadata,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,6 +162,41 @@ impl Default for Session {
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionMetadata {
|
||||
#[must_use]
|
||||
pub fn to_json(&self) -> JsonValue {
|
||||
let mut object = BTreeMap::new();
|
||||
object.insert(
|
||||
"started_at".to_string(),
|
||||
JsonValue::String(self.started_at.clone()),
|
||||
);
|
||||
object.insert("model".to_string(), JsonValue::String(self.model.clone()));
|
||||
object.insert(
|
||||
"message_count".to_string(),
|
||||
JsonValue::Number(i64::from(self.message_count)),
|
||||
);
|
||||
if let Some(last_prompt) = &self.last_prompt {
|
||||
object.insert(
|
||||
"last_prompt".to_string(),
|
||||
JsonValue::String(last_prompt.clone()),
|
||||
);
|
||||
}
|
||||
JsonValue::Object(object)
|
||||
}
|
||||
|
||||
fn from_json(value: &JsonValue) -> Result<Self, SessionError> {
|
||||
let object = value.as_object().ok_or_else(|| {
|
||||
SessionError::Format("session metadata must be an object".to_string())
|
||||
})?;
|
||||
Ok(Self {
|
||||
started_at: required_string(object, "started_at")?,
|
||||
model: required_string(object, "model")?,
|
||||
message_count: required_u32(object, "message_count")?,
|
||||
last_prompt: optional_string(object, "last_prompt"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ConversationMessage {
|
||||
#[must_use]
|
||||
pub fn user_text(text: impl Into<String>) -> Self {
|
||||
@@ -368,6 +424,13 @@ fn required_string(
|
||||
.ok_or_else(|| SessionError::Format(format!("missing {key}")))
|
||||
}
|
||||
|
||||
fn optional_string(object: &BTreeMap<String, JsonValue>, key: &str) -> Option<String> {
|
||||
object
|
||||
.get(key)
|
||||
.and_then(JsonValue::as_str)
|
||||
.map(ToOwned::to_owned)
|
||||
}
|
||||
|
||||
fn required_u32(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u32, SessionError> {
|
||||
let value = object
|
||||
.get(key)
|
||||
@@ -378,7 +441,8 @@ fn required_u32(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u32,
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{ContentBlock, ConversationMessage, MessageRole, Session};
|
||||
use super::{ContentBlock, ConversationMessage, MessageRole, Session, SessionMetadata};
|
||||
use crate::json::JsonValue;
|
||||
use crate::usage::TokenUsage;
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
@@ -386,6 +450,12 @@ mod tests {
|
||||
#[test]
|
||||
fn persists_and_restores_session_json() {
|
||||
let mut session = Session::new();
|
||||
session.metadata = Some(SessionMetadata {
|
||||
started_at: "2026-04-01T00:00:00Z".to_string(),
|
||||
model: "claude-sonnet".to_string(),
|
||||
message_count: 3,
|
||||
last_prompt: Some("hello".to_string()),
|
||||
});
|
||||
session
|
||||
.messages
|
||||
.push(ConversationMessage::user_text("hello"));
|
||||
@@ -428,5 +498,23 @@ mod tests {
|
||||
restored.messages[1].usage.expect("usage").total_tokens(),
|
||||
17
|
||||
);
|
||||
assert_eq!(restored.metadata, session.metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loads_legacy_session_without_metadata() {
|
||||
let legacy = r#"{
|
||||
"version": 1,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"blocks": [{"type": "text", "text": "hello"}]
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
let restored = Session::from_json(&JsonValue::parse(legacy).expect("legacy json"))
|
||||
.expect("legacy session should parse");
|
||||
assert_eq!(restored.messages.len(), 1);
|
||||
assert!(restored.metadata.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,6 +300,7 @@ mod tests {
|
||||
cache_read_input_tokens: 0,
|
||||
}),
|
||||
}],
|
||||
metadata: None,
|
||||
};
|
||||
|
||||
let tracker = UsageTracker::from_session(&session);
|
||||
|
||||
Reference in New Issue
Block a user