mirror of
https://github.com/instructkr/claw-code.git
synced 2026-04-03 21:44:49 +08:00
Compare commits
3 Commits
rcc/api
...
rcc/runtim
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccebabe605 | ||
|
|
146260083c | ||
|
|
cd01d0e387 |
@@ -1,127 +0,0 @@
|
|||||||
{
|
|
||||||
"version": "1.0.0",
|
|
||||||
"lastScanned": 1774971516826,
|
|
||||||
"projectRoot": "/home/bellman/Workspace/clawd-code-worktrees/api/rust",
|
|
||||||
"techStack": {
|
|
||||||
"languages": [
|
|
||||||
{
|
|
||||||
"name": "Rust",
|
|
||||||
"version": null,
|
|
||||||
"confidence": "high",
|
|
||||||
"markers": [
|
|
||||||
"Cargo.toml"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"frameworks": [],
|
|
||||||
"packageManager": "cargo",
|
|
||||||
"runtime": null
|
|
||||||
},
|
|
||||||
"build": {
|
|
||||||
"buildCommand": "cargo build",
|
|
||||||
"testCommand": "cargo test",
|
|
||||||
"lintCommand": "cargo clippy",
|
|
||||||
"devCommand": "cargo run",
|
|
||||||
"scripts": {}
|
|
||||||
},
|
|
||||||
"conventions": {
|
|
||||||
"namingStyle": null,
|
|
||||||
"importStyle": null,
|
|
||||||
"testPattern": null,
|
|
||||||
"fileOrganization": null
|
|
||||||
},
|
|
||||||
"structure": {
|
|
||||||
"isMonorepo": false,
|
|
||||||
"workspaces": [],
|
|
||||||
"mainDirectories": [],
|
|
||||||
"gitBranches": {
|
|
||||||
"defaultBranch": "main",
|
|
||||||
"branchingStrategy": null
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"customNotes": [],
|
|
||||||
"directoryMap": {
|
|
||||||
"crates": {
|
|
||||||
"path": "crates",
|
|
||||||
"purpose": null,
|
|
||||||
"fileCount": 0,
|
|
||||||
"lastAccessed": 1774971516823,
|
|
||||||
"keyFiles": []
|
|
||||||
},
|
|
||||||
"target": {
|
|
||||||
"path": "target",
|
|
||||||
"purpose": null,
|
|
||||||
"fileCount": 2,
|
|
||||||
"lastAccessed": 1774971516823,
|
|
||||||
"keyFiles": [
|
|
||||||
"CACHEDIR.TAG"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"crates/api": {
|
|
||||||
"path": "crates/api",
|
|
||||||
"purpose": "API routes",
|
|
||||||
"fileCount": 1,
|
|
||||||
"lastAccessed": 1774971516824,
|
|
||||||
"keyFiles": [
|
|
||||||
"Cargo.toml"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"hotPaths": [
|
|
||||||
{
|
|
||||||
"path": "crates/api/Cargo.toml",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971547109,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/src/lib.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971547624,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/src/client.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971548001,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/src/error.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971548503,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/src/sse.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971549311,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/src/types.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971549472,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/api/tests/client_integration.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971550143,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "Cargo.toml",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971550539,
|
|
||||||
"type": "file"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "crates/rusty-claude-cli/src/main.rs",
|
|
||||||
"accessCount": 1,
|
|
||||||
"lastAccessed": 1774971551474,
|
|
||||||
"type": "file"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"userDirectives": []
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"lastSentAt": "2026-03-31T15:39:44.771Z"
|
|
||||||
}
|
|
||||||
@@ -109,13 +109,6 @@ cd rust
|
|||||||
cargo run -p rusty-claude-cli -- --allowedTools read,glob
|
cargo run -p rusty-claude-cli -- --allowedTools read,glob
|
||||||
```
|
```
|
||||||
|
|
||||||
Bootstrap Claude project files for the current repo:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd rust
|
|
||||||
cargo run -p rusty-claude-cli -- init
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2) REPL mode
|
### 2) REPL mode
|
||||||
|
|
||||||
Start the interactive shell:
|
Start the interactive shell:
|
||||||
@@ -140,6 +133,7 @@ Inside the REPL, useful commands include:
|
|||||||
/diff
|
/diff
|
||||||
/version
|
/version
|
||||||
/export notes.txt
|
/export notes.txt
|
||||||
|
/sessions
|
||||||
/session list
|
/session list
|
||||||
/exit
|
/exit
|
||||||
```
|
```
|
||||||
@@ -150,14 +144,14 @@ Inspect or maintain a saved session file without entering the REPL:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd rust
|
cd rust
|
||||||
cargo run -p rusty-claude-cli -- --resume session.json /status /compact /cost
|
cargo run -p rusty-claude-cli -- --resume session-123456 /status /compact /cost
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also inspect memory/config state for a restored session:
|
You can also inspect memory/config state for a restored session:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd rust
|
cd rust
|
||||||
cargo run -p rusty-claude-cli -- --resume session.json /memory /config
|
cargo run -p rusty-claude-cli -- --resume ~/.claude/sessions/session-123456.json /memory /config
|
||||||
```
|
```
|
||||||
|
|
||||||
## Available commands
|
## Available commands
|
||||||
@@ -165,7 +159,7 @@ cargo run -p rusty-claude-cli -- --resume session.json /memory /config
|
|||||||
### Top-level CLI commands
|
### Top-level CLI commands
|
||||||
|
|
||||||
- `prompt <text...>` — run one prompt non-interactively
|
- `prompt <text...>` — run one prompt non-interactively
|
||||||
- `--resume <session.json> [/commands...]` — inspect or maintain a saved session
|
- `--resume <session-id-or-path> [/commands...]` — inspect or maintain a saved session stored under `~/.claude/sessions/`
|
||||||
- `dump-manifests` — print extracted upstream manifest counts
|
- `dump-manifests` — print extracted upstream manifest counts
|
||||||
- `bootstrap-plan` — print the current bootstrap skeleton
|
- `bootstrap-plan` — print the current bootstrap skeleton
|
||||||
- `system-prompt [--cwd PATH] [--date YYYY-MM-DD]` — render the synthesized system prompt
|
- `system-prompt [--cwd PATH] [--date YYYY-MM-DD]` — render the synthesized system prompt
|
||||||
@@ -183,13 +177,14 @@ cargo run -p rusty-claude-cli -- --resume session.json /memory /config
|
|||||||
- `/permissions [read-only|workspace-write|danger-full-access]` — inspect or switch permissions
|
- `/permissions [read-only|workspace-write|danger-full-access]` — inspect or switch permissions
|
||||||
- `/clear [--confirm]` — clear the current local session
|
- `/clear [--confirm]` — clear the current local session
|
||||||
- `/cost` — show token usage totals
|
- `/cost` — show token usage totals
|
||||||
- `/resume <session-path>` — load a saved session into the REPL
|
- `/resume <session-id-or-path>` — load a saved session into the REPL
|
||||||
- `/config [env|hooks|model]` — inspect discovered Claude config
|
- `/config [env|hooks|model]` — inspect discovered Claude config
|
||||||
- `/memory` — inspect loaded instruction memory files
|
- `/memory` — inspect loaded instruction memory files
|
||||||
- `/init` — bootstrap `.claude.json`, `.claude/`, `CLAUDE.md`, and local ignore rules
|
- `/init` — create a starter `CLAUDE.md`
|
||||||
- `/diff` — show the current git diff for the workspace
|
- `/diff` — show the current git diff for the workspace
|
||||||
- `/version` — print version and build metadata locally
|
- `/version` — print version and build metadata locally
|
||||||
- `/export [file]` — export the current conversation transcript
|
- `/export [file]` — export the current conversation transcript
|
||||||
|
- `/sessions` — list recent managed local sessions from `~/.claude/sessions/`
|
||||||
- `/session [list|switch <session-id>]` — inspect or switch managed local sessions
|
- `/session [list|switch <session-id>]` — inspect or switch managed local sessions
|
||||||
- `/exit` — leave the REPL
|
- `/exit` — leave the REPL
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
|
|||||||
SlashCommandSpec {
|
SlashCommandSpec {
|
||||||
name: "resume",
|
name: "resume",
|
||||||
summary: "Load a saved session into the REPL",
|
summary: "Load a saved session into the REPL",
|
||||||
argument_hint: Some("<session-path>"),
|
argument_hint: Some("<session-id-or-path>"),
|
||||||
resume_supported: false,
|
resume_supported: false,
|
||||||
},
|
},
|
||||||
SlashCommandSpec {
|
SlashCommandSpec {
|
||||||
@@ -101,7 +101,7 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
|
|||||||
},
|
},
|
||||||
SlashCommandSpec {
|
SlashCommandSpec {
|
||||||
name: "init",
|
name: "init",
|
||||||
summary: "Bootstrap Claude project files for this repo",
|
summary: "Create a starter CLAUDE.md for this repo",
|
||||||
argument_hint: None,
|
argument_hint: None,
|
||||||
resume_supported: true,
|
resume_supported: true,
|
||||||
},
|
},
|
||||||
@@ -129,6 +129,12 @@ const SLASH_COMMAND_SPECS: &[SlashCommandSpec] = &[
|
|||||||
argument_hint: Some("[list|switch <session-id>]"),
|
argument_hint: Some("[list|switch <session-id>]"),
|
||||||
resume_supported: false,
|
resume_supported: false,
|
||||||
},
|
},
|
||||||
|
SlashCommandSpec {
|
||||||
|
name: "sessions",
|
||||||
|
summary: "List recent managed local sessions",
|
||||||
|
argument_hint: None,
|
||||||
|
resume_supported: false,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@@ -163,6 +169,7 @@ pub enum SlashCommand {
|
|||||||
action: Option<String>,
|
action: Option<String>,
|
||||||
target: Option<String>,
|
target: Option<String>,
|
||||||
},
|
},
|
||||||
|
Sessions,
|
||||||
Unknown(String),
|
Unknown(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,6 +214,7 @@ impl SlashCommand {
|
|||||||
action: parts.next().map(ToOwned::to_owned),
|
action: parts.next().map(ToOwned::to_owned),
|
||||||
target: parts.next().map(ToOwned::to_owned),
|
target: parts.next().map(ToOwned::to_owned),
|
||||||
},
|
},
|
||||||
|
"sessions" => Self::Sessions,
|
||||||
other => Self::Unknown(other.to_string()),
|
other => Self::Unknown(other.to_string()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -291,6 +299,7 @@ pub fn handle_slash_command(
|
|||||||
| SlashCommand::Version
|
| SlashCommand::Version
|
||||||
| SlashCommand::Export { .. }
|
| SlashCommand::Export { .. }
|
||||||
| SlashCommand::Session { .. }
|
| SlashCommand::Session { .. }
|
||||||
|
| SlashCommand::Sessions
|
||||||
| SlashCommand::Unknown(_) => None,
|
| SlashCommand::Unknown(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -365,6 +374,10 @@ mod tests {
|
|||||||
target: Some("abc123".to_string())
|
target: Some("abc123".to_string())
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
SlashCommand::parse("/sessions"),
|
||||||
|
Some(SlashCommand::Sessions)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -378,7 +391,7 @@ mod tests {
|
|||||||
assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
|
assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
|
||||||
assert!(help.contains("/clear [--confirm]"));
|
assert!(help.contains("/clear [--confirm]"));
|
||||||
assert!(help.contains("/cost"));
|
assert!(help.contains("/cost"));
|
||||||
assert!(help.contains("/resume <session-path>"));
|
assert!(help.contains("/resume <session-id-or-path>"));
|
||||||
assert!(help.contains("/config [env|hooks|model]"));
|
assert!(help.contains("/config [env|hooks|model]"));
|
||||||
assert!(help.contains("/memory"));
|
assert!(help.contains("/memory"));
|
||||||
assert!(help.contains("/init"));
|
assert!(help.contains("/init"));
|
||||||
@@ -386,7 +399,8 @@ mod tests {
|
|||||||
assert!(help.contains("/version"));
|
assert!(help.contains("/version"));
|
||||||
assert!(help.contains("/export [file]"));
|
assert!(help.contains("/export [file]"));
|
||||||
assert!(help.contains("/session [list|switch <session-id>]"));
|
assert!(help.contains("/session [list|switch <session-id>]"));
|
||||||
assert_eq!(slash_command_specs().len(), 15);
|
assert!(help.contains("/sessions"));
|
||||||
|
assert_eq!(slash_command_specs().len(), 16);
|
||||||
assert_eq!(resume_supported_slash_commands().len(), 11);
|
assert_eq!(resume_supported_slash_commands().len(), 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,6 +418,7 @@ mod tests {
|
|||||||
text: "recent".to_string(),
|
text: "recent".to_string(),
|
||||||
}]),
|
}]),
|
||||||
],
|
],
|
||||||
|
metadata: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = handle_slash_command(
|
let result = handle_slash_command(
|
||||||
@@ -468,5 +483,6 @@ mod tests {
|
|||||||
assert!(
|
assert!(
|
||||||
handle_slash_command("/session list", &session, CompactionConfig::default()).is_none()
|
handle_slash_command("/session list", &session, CompactionConfig::default()).is_none()
|
||||||
);
|
);
|
||||||
|
assert!(handle_slash_command("/sessions", &session, CompactionConfig::default()).is_none());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ pub fn compact_session(session: &Session, config: CompactionConfig) -> Compactio
|
|||||||
compacted_session: Session {
|
compacted_session: Session {
|
||||||
version: session.version,
|
version: session.version,
|
||||||
messages: compacted_messages,
|
messages: compacted_messages,
|
||||||
|
metadata: session.metadata.clone(),
|
||||||
},
|
},
|
||||||
removed_message_count: removed.len(),
|
removed_message_count: removed.len(),
|
||||||
}
|
}
|
||||||
@@ -393,6 +394,7 @@ mod tests {
|
|||||||
let session = Session {
|
let session = Session {
|
||||||
version: 1,
|
version: 1,
|
||||||
messages: vec![ConversationMessage::user_text("hello")],
|
messages: vec![ConversationMessage::user_text("hello")],
|
||||||
|
metadata: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = compact_session(&session, CompactionConfig::default());
|
let result = compact_session(&session, CompactionConfig::default());
|
||||||
@@ -420,6 +422,7 @@ mod tests {
|
|||||||
usage: None,
|
usage: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
metadata: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = compact_session(
|
let result = compact_session(
|
||||||
|
|||||||
@@ -73,7 +73,9 @@ pub use remote::{
|
|||||||
RemoteSessionContext, UpstreamProxyBootstrap, UpstreamProxyState, DEFAULT_REMOTE_BASE_URL,
|
RemoteSessionContext, UpstreamProxyBootstrap, UpstreamProxyState, DEFAULT_REMOTE_BASE_URL,
|
||||||
DEFAULT_SESSION_TOKEN_PATH, DEFAULT_SYSTEM_CA_BUNDLE, NO_PROXY_HOSTS, UPSTREAM_PROXY_ENV_KEYS,
|
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::{
|
pub use usage::{
|
||||||
format_usd, pricing_for_model, ModelPricing, TokenUsage, UsageCostEstimate, UsageTracker,
|
format_usd, pricing_for_model, ModelPricing, TokenUsage, UsageCostEstimate, UsageTracker,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -39,10 +39,19 @@ pub struct ConversationMessage {
|
|||||||
pub usage: Option<TokenUsage>,
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
pub version: u32,
|
pub version: u32,
|
||||||
pub messages: Vec<ConversationMessage>,
|
pub messages: Vec<ConversationMessage>,
|
||||||
|
pub metadata: Option<SessionMetadata>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -82,6 +91,7 @@ impl Session {
|
|||||||
Self {
|
Self {
|
||||||
version: 1,
|
version: 1,
|
||||||
messages: Vec::new(),
|
messages: Vec::new(),
|
||||||
|
metadata: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,6 +121,9 @@ impl Session {
|
|||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
if let Some(metadata) = &self.metadata {
|
||||||
|
object.insert("metadata".to_string(), metadata.to_json());
|
||||||
|
}
|
||||||
JsonValue::Object(object)
|
JsonValue::Object(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +144,15 @@ impl Session {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(ConversationMessage::from_json)
|
.map(ConversationMessage::from_json)
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.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 {
|
impl ConversationMessage {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn user_text(text: impl Into<String>) -> Self {
|
pub fn user_text(text: impl Into<String>) -> Self {
|
||||||
@@ -368,6 +424,13 @@ fn required_string(
|
|||||||
.ok_or_else(|| SessionError::Format(format!("missing {key}")))
|
.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> {
|
fn required_u32(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u32, SessionError> {
|
||||||
let value = object
|
let value = object
|
||||||
.get(key)
|
.get(key)
|
||||||
@@ -378,7 +441,8 @@ fn required_u32(object: &BTreeMap<String, JsonValue>, key: &str) -> Result<u32,
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{ContentBlock, ConversationMessage, MessageRole, Session};
|
use super::{ContentBlock, ConversationMessage, MessageRole, Session, SessionMetadata};
|
||||||
|
use crate::json::JsonValue;
|
||||||
use crate::usage::TokenUsage;
|
use crate::usage::TokenUsage;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
@@ -386,6 +450,12 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn persists_and_restores_session_json() {
|
fn persists_and_restores_session_json() {
|
||||||
let mut session = Session::new();
|
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
|
session
|
||||||
.messages
|
.messages
|
||||||
.push(ConversationMessage::user_text("hello"));
|
.push(ConversationMessage::user_text("hello"));
|
||||||
@@ -428,5 +498,23 @@ mod tests {
|
|||||||
restored.messages[1].usage.expect("usage").total_tokens(),
|
restored.messages[1].usage.expect("usage").total_tokens(),
|
||||||
17
|
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,
|
cache_read_input_tokens: 0,
|
||||||
}),
|
}),
|
||||||
}],
|
}],
|
||||||
|
metadata: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let tracker = UsageTracker::from_session(&session);
|
let tracker = UsageTracker::from_session(&session);
|
||||||
|
|||||||
@@ -1,433 +0,0 @@
|
|||||||
use std::fs;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
const STARTER_CLAUDE_JSON: &str = concat!(
|
|
||||||
"{\n",
|
|
||||||
" \"permissions\": {\n",
|
|
||||||
" \"defaultMode\": \"acceptEdits\"\n",
|
|
||||||
" }\n",
|
|
||||||
"}\n",
|
|
||||||
);
|
|
||||||
const GITIGNORE_COMMENT: &str = "# Claude Code local artifacts";
|
|
||||||
const GITIGNORE_ENTRIES: [&str; 2] = [".claude/settings.local.json", ".claude/sessions/"];
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub(crate) enum InitStatus {
|
|
||||||
Created,
|
|
||||||
Updated,
|
|
||||||
Skipped,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InitStatus {
|
|
||||||
#[must_use]
|
|
||||||
pub(crate) fn label(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::Created => "created",
|
|
||||||
Self::Updated => "updated",
|
|
||||||
Self::Skipped => "skipped (already exists)",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub(crate) struct InitArtifact {
|
|
||||||
pub(crate) name: &'static str,
|
|
||||||
pub(crate) status: InitStatus,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub(crate) struct InitReport {
|
|
||||||
pub(crate) project_root: PathBuf,
|
|
||||||
pub(crate) artifacts: Vec<InitArtifact>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InitReport {
|
|
||||||
#[must_use]
|
|
||||||
pub(crate) fn render(&self) -> String {
|
|
||||||
let mut lines = vec![
|
|
||||||
"Init".to_string(),
|
|
||||||
format!(" Project {}", self.project_root.display()),
|
|
||||||
];
|
|
||||||
for artifact in &self.artifacts {
|
|
||||||
lines.push(format!(
|
|
||||||
" {:<16} {}",
|
|
||||||
artifact.name,
|
|
||||||
artifact.status.label()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
lines.push(" Next step Review and tailor the generated guidance".to_string());
|
|
||||||
lines.join("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
||||||
#[allow(clippy::struct_excessive_bools)]
|
|
||||||
struct RepoDetection {
|
|
||||||
rust_workspace: bool,
|
|
||||||
rust_root: bool,
|
|
||||||
python: bool,
|
|
||||||
package_json: bool,
|
|
||||||
typescript: bool,
|
|
||||||
nextjs: bool,
|
|
||||||
react: bool,
|
|
||||||
vite: bool,
|
|
||||||
nest: bool,
|
|
||||||
src_dir: bool,
|
|
||||||
tests_dir: bool,
|
|
||||||
rust_dir: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn initialize_repo(cwd: &Path) -> Result<InitReport, Box<dyn std::error::Error>> {
|
|
||||||
let mut artifacts = Vec::new();
|
|
||||||
|
|
||||||
let claude_dir = cwd.join(".claude");
|
|
||||||
artifacts.push(InitArtifact {
|
|
||||||
name: ".claude/",
|
|
||||||
status: ensure_dir(&claude_dir)?,
|
|
||||||
});
|
|
||||||
|
|
||||||
let claude_json = cwd.join(".claude.json");
|
|
||||||
artifacts.push(InitArtifact {
|
|
||||||
name: ".claude.json",
|
|
||||||
status: write_file_if_missing(&claude_json, STARTER_CLAUDE_JSON)?,
|
|
||||||
});
|
|
||||||
|
|
||||||
let gitignore = cwd.join(".gitignore");
|
|
||||||
artifacts.push(InitArtifact {
|
|
||||||
name: ".gitignore",
|
|
||||||
status: ensure_gitignore_entries(&gitignore)?,
|
|
||||||
});
|
|
||||||
|
|
||||||
let claude_md = cwd.join("CLAUDE.md");
|
|
||||||
let content = render_init_claude_md(cwd);
|
|
||||||
artifacts.push(InitArtifact {
|
|
||||||
name: "CLAUDE.md",
|
|
||||||
status: write_file_if_missing(&claude_md, &content)?,
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(InitReport {
|
|
||||||
project_root: cwd.to_path_buf(),
|
|
||||||
artifacts,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ensure_dir(path: &Path) -> Result<InitStatus, std::io::Error> {
|
|
||||||
if path.is_dir() {
|
|
||||||
return Ok(InitStatus::Skipped);
|
|
||||||
}
|
|
||||||
fs::create_dir_all(path)?;
|
|
||||||
Ok(InitStatus::Created)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_file_if_missing(path: &Path, content: &str) -> Result<InitStatus, std::io::Error> {
|
|
||||||
if path.exists() {
|
|
||||||
return Ok(InitStatus::Skipped);
|
|
||||||
}
|
|
||||||
fs::write(path, content)?;
|
|
||||||
Ok(InitStatus::Created)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ensure_gitignore_entries(path: &Path) -> Result<InitStatus, std::io::Error> {
|
|
||||||
if !path.exists() {
|
|
||||||
let mut lines = vec![GITIGNORE_COMMENT.to_string()];
|
|
||||||
lines.extend(GITIGNORE_ENTRIES.iter().map(|entry| (*entry).to_string()));
|
|
||||||
fs::write(path, format!("{}\n", lines.join("\n")))?;
|
|
||||||
return Ok(InitStatus::Created);
|
|
||||||
}
|
|
||||||
|
|
||||||
let existing = fs::read_to_string(path)?;
|
|
||||||
let mut lines = existing.lines().map(ToOwned::to_owned).collect::<Vec<_>>();
|
|
||||||
let mut changed = false;
|
|
||||||
|
|
||||||
if !lines.iter().any(|line| line == GITIGNORE_COMMENT) {
|
|
||||||
lines.push(GITIGNORE_COMMENT.to_string());
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for entry in GITIGNORE_ENTRIES {
|
|
||||||
if !lines.iter().any(|line| line == entry) {
|
|
||||||
lines.push(entry.to_string());
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !changed {
|
|
||||||
return Ok(InitStatus::Skipped);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::write(path, format!("{}\n", lines.join("\n")))?;
|
|
||||||
Ok(InitStatus::Updated)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn render_init_claude_md(cwd: &Path) -> String {
|
|
||||||
let detection = detect_repo(cwd);
|
|
||||||
let mut lines = vec![
|
|
||||||
"# CLAUDE.md".to_string(),
|
|
||||||
String::new(),
|
|
||||||
"This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.".to_string(),
|
|
||||||
String::new(),
|
|
||||||
];
|
|
||||||
|
|
||||||
let detected_languages = detected_languages(&detection);
|
|
||||||
let detected_frameworks = detected_frameworks(&detection);
|
|
||||||
lines.push("## Detected stack".to_string());
|
|
||||||
if detected_languages.is_empty() {
|
|
||||||
lines.push("- No specific language markers were detected yet; document the primary language and verification commands once the project structure settles.".to_string());
|
|
||||||
} else {
|
|
||||||
lines.push(format!("- Languages: {}.", detected_languages.join(", ")));
|
|
||||||
}
|
|
||||||
if detected_frameworks.is_empty() {
|
|
||||||
lines.push("- Frameworks: none detected from the supported starter markers.".to_string());
|
|
||||||
} else {
|
|
||||||
lines.push(format!(
|
|
||||||
"- Frameworks/tooling markers: {}.",
|
|
||||||
detected_frameworks.join(", ")
|
|
||||||
));
|
|
||||||
}
|
|
||||||
lines.push(String::new());
|
|
||||||
|
|
||||||
let verification_lines = verification_lines(cwd, &detection);
|
|
||||||
if !verification_lines.is_empty() {
|
|
||||||
lines.push("## Verification".to_string());
|
|
||||||
lines.extend(verification_lines);
|
|
||||||
lines.push(String::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
let structure_lines = repository_shape_lines(&detection);
|
|
||||||
if !structure_lines.is_empty() {
|
|
||||||
lines.push("## Repository shape".to_string());
|
|
||||||
lines.extend(structure_lines);
|
|
||||||
lines.push(String::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
let framework_lines = framework_notes(&detection);
|
|
||||||
if !framework_lines.is_empty() {
|
|
||||||
lines.push("## Framework notes".to_string());
|
|
||||||
lines.extend(framework_lines);
|
|
||||||
lines.push(String::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
lines.push("## Working agreement".to_string());
|
|
||||||
lines.push("- Prefer small, reviewable changes and keep generated bootstrap files aligned with actual repo workflows.".to_string());
|
|
||||||
lines.push("- Keep shared defaults in `.claude.json`; reserve `.claude/settings.local.json` for machine-local overrides.".to_string());
|
|
||||||
lines.push("- Do not overwrite existing `CLAUDE.md` content automatically; update it intentionally when repo workflows change.".to_string());
|
|
||||||
lines.push(String::new());
|
|
||||||
|
|
||||||
lines.join("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn detect_repo(cwd: &Path) -> RepoDetection {
|
|
||||||
let package_json_contents = fs::read_to_string(cwd.join("package.json"))
|
|
||||||
.unwrap_or_default()
|
|
||||||
.to_ascii_lowercase();
|
|
||||||
RepoDetection {
|
|
||||||
rust_workspace: cwd.join("rust").join("Cargo.toml").is_file(),
|
|
||||||
rust_root: cwd.join("Cargo.toml").is_file(),
|
|
||||||
python: cwd.join("pyproject.toml").is_file()
|
|
||||||
|| cwd.join("requirements.txt").is_file()
|
|
||||||
|| cwd.join("setup.py").is_file(),
|
|
||||||
package_json: cwd.join("package.json").is_file(),
|
|
||||||
typescript: cwd.join("tsconfig.json").is_file()
|
|
||||||
|| package_json_contents.contains("typescript"),
|
|
||||||
nextjs: package_json_contents.contains("\"next\""),
|
|
||||||
react: package_json_contents.contains("\"react\""),
|
|
||||||
vite: package_json_contents.contains("\"vite\""),
|
|
||||||
nest: package_json_contents.contains("@nestjs"),
|
|
||||||
src_dir: cwd.join("src").is_dir(),
|
|
||||||
tests_dir: cwd.join("tests").is_dir(),
|
|
||||||
rust_dir: cwd.join("rust").is_dir(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn detected_languages(detection: &RepoDetection) -> Vec<&'static str> {
|
|
||||||
let mut languages = Vec::new();
|
|
||||||
if detection.rust_workspace || detection.rust_root {
|
|
||||||
languages.push("Rust");
|
|
||||||
}
|
|
||||||
if detection.python {
|
|
||||||
languages.push("Python");
|
|
||||||
}
|
|
||||||
if detection.typescript {
|
|
||||||
languages.push("TypeScript");
|
|
||||||
} else if detection.package_json {
|
|
||||||
languages.push("JavaScript/Node.js");
|
|
||||||
}
|
|
||||||
languages
|
|
||||||
}
|
|
||||||
|
|
||||||
fn detected_frameworks(detection: &RepoDetection) -> Vec<&'static str> {
|
|
||||||
let mut frameworks = Vec::new();
|
|
||||||
if detection.nextjs {
|
|
||||||
frameworks.push("Next.js");
|
|
||||||
}
|
|
||||||
if detection.react {
|
|
||||||
frameworks.push("React");
|
|
||||||
}
|
|
||||||
if detection.vite {
|
|
||||||
frameworks.push("Vite");
|
|
||||||
}
|
|
||||||
if detection.nest {
|
|
||||||
frameworks.push("NestJS");
|
|
||||||
}
|
|
||||||
frameworks
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verification_lines(cwd: &Path, detection: &RepoDetection) -> Vec<String> {
|
|
||||||
let mut lines = Vec::new();
|
|
||||||
if detection.rust_workspace {
|
|
||||||
lines.push("- Run Rust verification from `rust/`: `cargo fmt`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`".to_string());
|
|
||||||
} else if detection.rust_root {
|
|
||||||
lines.push("- Run Rust verification from the repo root: `cargo fmt`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`".to_string());
|
|
||||||
}
|
|
||||||
if detection.python {
|
|
||||||
if cwd.join("pyproject.toml").is_file() {
|
|
||||||
lines.push("- Run the Python project checks declared in `pyproject.toml` (for example: `pytest`, `ruff check`, and `mypy` when configured).".to_string());
|
|
||||||
} else {
|
|
||||||
lines.push(
|
|
||||||
"- Run the repo's Python test/lint commands before shipping changes.".to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if detection.package_json {
|
|
||||||
lines.push("- Run the JavaScript/TypeScript checks from `package.json` before shipping changes (`npm test`, `npm run lint`, `npm run build`, or the repo equivalent).".to_string());
|
|
||||||
}
|
|
||||||
if detection.tests_dir && detection.src_dir {
|
|
||||||
lines.push("- `src/` and `tests/` are both present; update both surfaces together when behavior changes.".to_string());
|
|
||||||
}
|
|
||||||
lines
|
|
||||||
}
|
|
||||||
|
|
||||||
fn repository_shape_lines(detection: &RepoDetection) -> Vec<String> {
|
|
||||||
let mut lines = Vec::new();
|
|
||||||
if detection.rust_dir {
|
|
||||||
lines.push(
|
|
||||||
"- `rust/` contains the Rust workspace and active CLI/runtime implementation."
|
|
||||||
.to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if detection.src_dir {
|
|
||||||
lines.push("- `src/` contains source files that should stay consistent with generated guidance and tests.".to_string());
|
|
||||||
}
|
|
||||||
if detection.tests_dir {
|
|
||||||
lines.push("- `tests/` contains validation surfaces that should be reviewed alongside code changes.".to_string());
|
|
||||||
}
|
|
||||||
lines
|
|
||||||
}
|
|
||||||
|
|
||||||
fn framework_notes(detection: &RepoDetection) -> Vec<String> {
|
|
||||||
let mut lines = Vec::new();
|
|
||||||
if detection.nextjs {
|
|
||||||
lines.push("- Next.js detected: preserve routing/data-fetching conventions and verify production builds after changing app structure.".to_string());
|
|
||||||
}
|
|
||||||
if detection.react && !detection.nextjs {
|
|
||||||
lines.push("- React detected: keep component behavior covered with focused tests and avoid unnecessary prop/API churn.".to_string());
|
|
||||||
}
|
|
||||||
if detection.vite {
|
|
||||||
lines.push("- Vite detected: validate the production bundle after changing build-sensitive configuration or imports.".to_string());
|
|
||||||
}
|
|
||||||
if detection.nest {
|
|
||||||
lines.push("- NestJS detected: keep module/provider boundaries explicit and verify controller/service wiring after refactors.".to_string());
|
|
||||||
}
|
|
||||||
lines
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::{initialize_repo, render_init_claude_md};
|
|
||||||
use std::fs;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
fn temp_dir() -> std::path::PathBuf {
|
|
||||||
let nanos = SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("time should be after epoch")
|
|
||||||
.as_nanos();
|
|
||||||
std::env::temp_dir().join(format!("rusty-claude-init-{nanos}"))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn initialize_repo_creates_expected_files_and_gitignore_entries() {
|
|
||||||
let root = temp_dir();
|
|
||||||
fs::create_dir_all(root.join("rust")).expect("create rust dir");
|
|
||||||
fs::write(root.join("rust").join("Cargo.toml"), "[workspace]\n").expect("write cargo");
|
|
||||||
|
|
||||||
let report = initialize_repo(&root).expect("init should succeed");
|
|
||||||
let rendered = report.render();
|
|
||||||
assert!(rendered.contains(".claude/ created"));
|
|
||||||
assert!(rendered.contains(".claude.json created"));
|
|
||||||
assert!(rendered.contains(".gitignore created"));
|
|
||||||
assert!(rendered.contains("CLAUDE.md created"));
|
|
||||||
assert!(root.join(".claude").is_dir());
|
|
||||||
assert!(root.join(".claude.json").is_file());
|
|
||||||
assert!(root.join("CLAUDE.md").is_file());
|
|
||||||
assert_eq!(
|
|
||||||
fs::read_to_string(root.join(".claude.json")).expect("read claude json"),
|
|
||||||
concat!(
|
|
||||||
"{\n",
|
|
||||||
" \"permissions\": {\n",
|
|
||||||
" \"defaultMode\": \"acceptEdits\"\n",
|
|
||||||
" }\n",
|
|
||||||
"}\n",
|
|
||||||
)
|
|
||||||
);
|
|
||||||
let gitignore = fs::read_to_string(root.join(".gitignore")).expect("read gitignore");
|
|
||||||
assert!(gitignore.contains(".claude/settings.local.json"));
|
|
||||||
assert!(gitignore.contains(".claude/sessions/"));
|
|
||||||
let claude_md = fs::read_to_string(root.join("CLAUDE.md")).expect("read claude md");
|
|
||||||
assert!(claude_md.contains("Languages: Rust."));
|
|
||||||
assert!(claude_md.contains("cargo clippy --workspace --all-targets -- -D warnings"));
|
|
||||||
|
|
||||||
fs::remove_dir_all(root).expect("cleanup temp dir");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn initialize_repo_is_idempotent_and_preserves_existing_files() {
|
|
||||||
let root = temp_dir();
|
|
||||||
fs::create_dir_all(&root).expect("create root");
|
|
||||||
fs::write(root.join("CLAUDE.md"), "custom guidance\n").expect("write existing claude md");
|
|
||||||
fs::write(root.join(".gitignore"), ".claude/settings.local.json\n")
|
|
||||||
.expect("write gitignore");
|
|
||||||
|
|
||||||
let first = initialize_repo(&root).expect("first init should succeed");
|
|
||||||
assert!(first
|
|
||||||
.render()
|
|
||||||
.contains("CLAUDE.md skipped (already exists)"));
|
|
||||||
let second = initialize_repo(&root).expect("second init should succeed");
|
|
||||||
let second_rendered = second.render();
|
|
||||||
assert!(second_rendered.contains(".claude/ skipped (already exists)"));
|
|
||||||
assert!(second_rendered.contains(".claude.json skipped (already exists)"));
|
|
||||||
assert!(second_rendered.contains(".gitignore skipped (already exists)"));
|
|
||||||
assert!(second_rendered.contains("CLAUDE.md skipped (already exists)"));
|
|
||||||
assert_eq!(
|
|
||||||
fs::read_to_string(root.join("CLAUDE.md")).expect("read existing claude md"),
|
|
||||||
"custom guidance\n"
|
|
||||||
);
|
|
||||||
let gitignore = fs::read_to_string(root.join(".gitignore")).expect("read gitignore");
|
|
||||||
assert_eq!(gitignore.matches(".claude/settings.local.json").count(), 1);
|
|
||||||
assert_eq!(gitignore.matches(".claude/sessions/").count(), 1);
|
|
||||||
|
|
||||||
fs::remove_dir_all(root).expect("cleanup temp dir");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn render_init_template_mentions_detected_python_and_nextjs_markers() {
|
|
||||||
let root = temp_dir();
|
|
||||||
fs::create_dir_all(&root).expect("create root");
|
|
||||||
fs::write(root.join("pyproject.toml"), "[project]\nname = \"demo\"\n")
|
|
||||||
.expect("write pyproject");
|
|
||||||
fs::write(
|
|
||||||
root.join("package.json"),
|
|
||||||
r#"{"dependencies":{"next":"14.0.0","react":"18.0.0"},"devDependencies":{"typescript":"5.0.0"}}"#,
|
|
||||||
)
|
|
||||||
.expect("write package json");
|
|
||||||
|
|
||||||
let rendered = render_init_claude_md(Path::new(&root));
|
|
||||||
assert!(rendered.contains("Languages: Python, TypeScript."));
|
|
||||||
assert!(rendered.contains("Frameworks/tooling markers: Next.js, React."));
|
|
||||||
assert!(rendered.contains("pyproject.toml"));
|
|
||||||
assert!(rendered.contains("Next.js detected"));
|
|
||||||
|
|
||||||
fs::remove_dir_all(root).expect("cleanup temp dir");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
mod init;
|
|
||||||
mod input;
|
mod input;
|
||||||
mod render;
|
mod render;
|
||||||
|
|
||||||
@@ -21,7 +20,6 @@ use commands::{
|
|||||||
render_slash_command_help, resume_supported_slash_commands, slash_command_specs, SlashCommand,
|
render_slash_command_help, resume_supported_slash_commands, slash_command_specs, SlashCommand,
|
||||||
};
|
};
|
||||||
use compat_harness::{extract_manifest, UpstreamPaths};
|
use compat_harness::{extract_manifest, UpstreamPaths};
|
||||||
use init::initialize_repo;
|
|
||||||
use render::{Spinner, TerminalRenderer};
|
use render::{Spinner, TerminalRenderer};
|
||||||
use runtime::{
|
use runtime::{
|
||||||
clear_oauth_credentials, generate_pkce_pair, generate_state, load_system_prompt,
|
clear_oauth_credentials, generate_pkce_pair, generate_state, load_system_prompt,
|
||||||
@@ -29,7 +27,7 @@ use runtime::{
|
|||||||
AssistantEvent, CompactionConfig, ConfigLoader, ConfigSource, ContentBlock,
|
AssistantEvent, CompactionConfig, ConfigLoader, ConfigSource, ContentBlock,
|
||||||
ConversationMessage, ConversationRuntime, MessageRole, OAuthAuthorizationRequest,
|
ConversationMessage, ConversationRuntime, MessageRole, OAuthAuthorizationRequest,
|
||||||
OAuthTokenExchangeRequest, PermissionMode, PermissionPolicy, ProjectContext, RuntimeError,
|
OAuthTokenExchangeRequest, PermissionMode, PermissionPolicy, ProjectContext, RuntimeError,
|
||||||
Session, TokenUsage, ToolError, ToolExecutor, UsageTracker,
|
Session, SessionMetadata, TokenUsage, ToolError, ToolExecutor, UsageTracker,
|
||||||
};
|
};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tools::{execute_tool, mvp_tool_specs, ToolSpec};
|
use tools::{execute_tool, mvp_tool_specs, ToolSpec};
|
||||||
@@ -39,6 +37,7 @@ const DEFAULT_MAX_TOKENS: u32 = 32;
|
|||||||
const DEFAULT_DATE: &str = "2026-03-31";
|
const DEFAULT_DATE: &str = "2026-03-31";
|
||||||
const DEFAULT_OAUTH_CALLBACK_PORT: u16 = 4545;
|
const DEFAULT_OAUTH_CALLBACK_PORT: u16 = 4545;
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
const OLD_SESSION_COMPACTION_AGE_SECS: u64 = 60 * 60 * 24;
|
||||||
const BUILD_TARGET: Option<&str> = option_env!("TARGET");
|
const BUILD_TARGET: Option<&str> = option_env!("TARGET");
|
||||||
const GIT_SHA: Option<&str> = option_env!("GIT_SHA");
|
const GIT_SHA: Option<&str> = option_env!("GIT_SHA");
|
||||||
|
|
||||||
@@ -76,7 +75,6 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.run_turn_with_output(&prompt, output_format)?,
|
.run_turn_with_output(&prompt, output_format)?,
|
||||||
CliAction::Login => run_login()?,
|
CliAction::Login => run_login()?,
|
||||||
CliAction::Logout => run_logout()?,
|
CliAction::Logout => run_logout()?,
|
||||||
CliAction::Init => run_init()?,
|
|
||||||
CliAction::Repl {
|
CliAction::Repl {
|
||||||
model,
|
model,
|
||||||
allowed_tools,
|
allowed_tools,
|
||||||
@@ -109,7 +107,6 @@ enum CliAction {
|
|||||||
},
|
},
|
||||||
Login,
|
Login,
|
||||||
Logout,
|
Logout,
|
||||||
Init,
|
|
||||||
Repl {
|
Repl {
|
||||||
model: String,
|
model: String,
|
||||||
allowed_tools: Option<AllowedToolSet>,
|
allowed_tools: Option<AllowedToolSet>,
|
||||||
@@ -234,7 +231,6 @@ fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
|||||||
"system-prompt" => parse_system_prompt_args(&rest[1..]),
|
"system-prompt" => parse_system_prompt_args(&rest[1..]),
|
||||||
"login" => Ok(CliAction::Login),
|
"login" => Ok(CliAction::Login),
|
||||||
"logout" => Ok(CliAction::Logout),
|
"logout" => Ok(CliAction::Logout),
|
||||||
"init" => Ok(CliAction::Init),
|
|
||||||
"prompt" => {
|
"prompt" => {
|
||||||
let prompt = rest[1..].join(" ");
|
let prompt = rest[1..].join(" ");
|
||||||
if prompt.trim().is_empty() {
|
if prompt.trim().is_empty() {
|
||||||
@@ -540,7 +536,14 @@ fn print_version() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn resume_session(session_path: &Path, commands: &[String]) {
|
fn resume_session(session_path: &Path, commands: &[String]) {
|
||||||
let session = match Session::load_from_path(session_path) {
|
let handle = match resolve_session_reference(&session_path.display().to_string()) {
|
||||||
|
Ok(handle) => handle,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("failed to resolve session: {error}");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let session = match Session::load_from_path(&handle.path) {
|
||||||
Ok(session) => session,
|
Ok(session) => session,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("failed to restore session: {error}");
|
eprintln!("failed to restore session: {error}");
|
||||||
@@ -551,7 +554,7 @@ fn resume_session(session_path: &Path, commands: &[String]) {
|
|||||||
if commands.is_empty() {
|
if commands.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"Restored session from {} ({} messages).",
|
"Restored session from {} ({} messages).",
|
||||||
session_path.display(),
|
handle.path.display(),
|
||||||
session.messages.len()
|
session.messages.len()
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@@ -563,7 +566,7 @@ fn resume_session(session_path: &Path, commands: &[String]) {
|
|||||||
eprintln!("unsupported resumed command: {raw_command}");
|
eprintln!("unsupported resumed command: {raw_command}");
|
||||||
std::process::exit(2);
|
std::process::exit(2);
|
||||||
};
|
};
|
||||||
match run_resume_command(session_path, &session, &command) {
|
match run_resume_command(&handle.path, &session, &command) {
|
||||||
Ok(ResumeCommandOutcome {
|
Ok(ResumeCommandOutcome {
|
||||||
session: next_session,
|
session: next_session,
|
||||||
message,
|
message,
|
||||||
@@ -708,6 +711,26 @@ fn format_resume_report(session_path: &str, message_count: usize, turns: u32) ->
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_init_report(path: &Path, created: bool) -> String {
|
||||||
|
if created {
|
||||||
|
format!(
|
||||||
|
"Init
|
||||||
|
CLAUDE.md {}
|
||||||
|
Result created
|
||||||
|
Next step Review and tailor the generated guidance",
|
||||||
|
path.display()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"Init
|
||||||
|
CLAUDE.md {}
|
||||||
|
Result skipped (already exists)
|
||||||
|
Next step Edit the existing file intentionally if workflows changed",
|
||||||
|
path.display()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn format_compact_report(removed: usize, resulting_messages: usize, skipped: bool) -> String {
|
fn format_compact_report(removed: usize, resulting_messages: usize, skipped: bool) -> String {
|
||||||
if skipped {
|
if skipped {
|
||||||
format!(
|
format!(
|
||||||
@@ -868,6 +891,7 @@ fn run_resume_command(
|
|||||||
| SlashCommand::Model { .. }
|
| SlashCommand::Model { .. }
|
||||||
| SlashCommand::Permissions { .. }
|
| SlashCommand::Permissions { .. }
|
||||||
| SlashCommand::Session { .. }
|
| SlashCommand::Session { .. }
|
||||||
|
| SlashCommand::Sessions
|
||||||
| SlashCommand::Unknown(_) => Err("unsupported resumed slash command".into()),
|
| SlashCommand::Unknown(_) => Err("unsupported resumed slash command".into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -924,6 +948,9 @@ struct ManagedSessionSummary {
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
modified_epoch_secs: u64,
|
modified_epoch_secs: u64,
|
||||||
message_count: usize,
|
message_count: usize,
|
||||||
|
model: Option<String>,
|
||||||
|
started_at: Option<String>,
|
||||||
|
last_prompt: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LiveCli {
|
struct LiveCli {
|
||||||
@@ -944,6 +971,7 @@ impl LiveCli {
|
|||||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let system_prompt = build_system_prompt()?;
|
let system_prompt = build_system_prompt()?;
|
||||||
let session = create_managed_session_handle()?;
|
let session = create_managed_session_handle()?;
|
||||||
|
auto_compact_inactive_sessions(&session.id)?;
|
||||||
let runtime = build_runtime(
|
let runtime = build_runtime(
|
||||||
Session::new(),
|
Session::new(),
|
||||||
model.clone(),
|
model.clone(),
|
||||||
@@ -1097,7 +1125,7 @@ impl LiveCli {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
SlashCommand::Init => {
|
SlashCommand::Init => {
|
||||||
run_init()?;
|
Self::run_init()?;
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
SlashCommand::Diff => {
|
SlashCommand::Diff => {
|
||||||
@@ -1115,6 +1143,10 @@ impl LiveCli {
|
|||||||
SlashCommand::Session { action, target } => {
|
SlashCommand::Session { action, target } => {
|
||||||
self.handle_session_command(action.as_deref(), target.as_deref())?
|
self.handle_session_command(action.as_deref(), target.as_deref())?
|
||||||
}
|
}
|
||||||
|
SlashCommand::Sessions => {
|
||||||
|
println!("{}", render_session_list(&self.session.id)?);
|
||||||
|
false
|
||||||
|
}
|
||||||
SlashCommand::Unknown(name) => {
|
SlashCommand::Unknown(name) => {
|
||||||
eprintln!("unknown slash command: /{name}");
|
eprintln!("unknown slash command: /{name}");
|
||||||
false
|
false
|
||||||
@@ -1123,7 +1155,10 @@ impl LiveCli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn persist_session(&self) -> Result<(), Box<dyn std::error::Error>> {
|
fn persist_session(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.runtime.session().save_to_path(&self.session.path)?;
|
let mut session = self.runtime.session().clone();
|
||||||
|
session.metadata = Some(derive_session_metadata(&session, &self.model));
|
||||||
|
session.save_to_path(&self.session.path)?;
|
||||||
|
auto_compact_inactive_sessions(&self.session.id)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1268,13 +1303,20 @@ impl LiveCli {
|
|||||||
session_path: Option<String>,
|
session_path: Option<String>,
|
||||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||||
let Some(session_ref) = session_path else {
|
let Some(session_ref) = session_path else {
|
||||||
println!("Usage: /resume <session-path>");
|
println!("Usage: /resume <session-id-or-path>");
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
let handle = resolve_session_reference(&session_ref)?;
|
let handle = resolve_session_reference(&session_ref)?;
|
||||||
let session = Session::load_from_path(&handle.path)?;
|
let session = Session::load_from_path(&handle.path)?;
|
||||||
let message_count = session.messages.len();
|
let message_count = session.messages.len();
|
||||||
|
if let Some(model) = session
|
||||||
|
.metadata
|
||||||
|
.as_ref()
|
||||||
|
.map(|metadata| metadata.model.clone())
|
||||||
|
{
|
||||||
|
self.model = model;
|
||||||
|
}
|
||||||
self.runtime = build_runtime(
|
self.runtime = build_runtime(
|
||||||
session,
|
session,
|
||||||
self.model.clone(),
|
self.model.clone(),
|
||||||
@@ -1305,6 +1347,11 @@ impl LiveCli {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_init() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("{}", init_claude_md()?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn print_diff() -> Result<(), Box<dyn std::error::Error>> {
|
fn print_diff() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("{}", render_diff_report()?);
|
println!("{}", render_diff_report()?);
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1346,6 +1393,13 @@ impl LiveCli {
|
|||||||
let handle = resolve_session_reference(target)?;
|
let handle = resolve_session_reference(target)?;
|
||||||
let session = Session::load_from_path(&handle.path)?;
|
let session = Session::load_from_path(&handle.path)?;
|
||||||
let message_count = session.messages.len();
|
let message_count = session.messages.len();
|
||||||
|
if let Some(model) = session
|
||||||
|
.metadata
|
||||||
|
.as_ref()
|
||||||
|
.map(|metadata| metadata.model.clone())
|
||||||
|
{
|
||||||
|
self.model = model;
|
||||||
|
}
|
||||||
self.runtime = build_runtime(
|
self.runtime = build_runtime(
|
||||||
session,
|
session,
|
||||||
self.model.clone(),
|
self.model.clone(),
|
||||||
@@ -1390,8 +1444,10 @@ impl LiveCli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn sessions_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
fn sessions_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||||
let cwd = env::current_dir()?;
|
let home = env::var_os("HOME")
|
||||||
let path = cwd.join(".claude").join("sessions");
|
.map(PathBuf::from)
|
||||||
|
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "HOME is not set"))?;
|
||||||
|
let path = home.join(".claude").join("sessions");
|
||||||
fs::create_dir_all(&path)?;
|
fs::create_dir_all(&path)?;
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
@@ -1412,8 +1468,19 @@ fn generate_session_id() -> String {
|
|||||||
|
|
||||||
fn resolve_session_reference(reference: &str) -> Result<SessionHandle, Box<dyn std::error::Error>> {
|
fn resolve_session_reference(reference: &str) -> Result<SessionHandle, Box<dyn std::error::Error>> {
|
||||||
let direct = PathBuf::from(reference);
|
let direct = PathBuf::from(reference);
|
||||||
|
let expanded = if let Some(stripped) = reference.strip_prefix("~/") {
|
||||||
|
sessions_dir()?
|
||||||
|
.parent()
|
||||||
|
.and_then(|claude| claude.parent())
|
||||||
|
.map(|home| home.join(stripped))
|
||||||
|
.unwrap_or(direct.clone())
|
||||||
|
} else {
|
||||||
|
direct.clone()
|
||||||
|
};
|
||||||
let path = if direct.exists() {
|
let path = if direct.exists() {
|
||||||
direct
|
direct
|
||||||
|
} else if expanded.exists() {
|
||||||
|
expanded
|
||||||
} else {
|
} else {
|
||||||
sessions_dir()?.join(format!("{reference}.json"))
|
sessions_dir()?.join(format!("{reference}.json"))
|
||||||
};
|
};
|
||||||
@@ -1443,9 +1510,11 @@ fn list_managed_sessions() -> Result<Vec<ManagedSessionSummary>, Box<dyn std::er
|
|||||||
.and_then(|time| time.duration_since(UNIX_EPOCH).ok())
|
.and_then(|time| time.duration_since(UNIX_EPOCH).ok())
|
||||||
.map(|duration| duration.as_secs())
|
.map(|duration| duration.as_secs())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let message_count = Session::load_from_path(&path)
|
let session = Session::load_from_path(&path).ok();
|
||||||
.map(|session| session.messages.len())
|
let derived_message_count = session.as_ref().map_or(0, |session| session.messages.len());
|
||||||
.unwrap_or_default();
|
let stored = session
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|session| session.metadata.as_ref());
|
||||||
let id = path
|
let id = path
|
||||||
.file_stem()
|
.file_stem()
|
||||||
.and_then(|value| value.to_str())
|
.and_then(|value| value.to_str())
|
||||||
@@ -1455,7 +1524,12 @@ fn list_managed_sessions() -> Result<Vec<ManagedSessionSummary>, Box<dyn std::er
|
|||||||
id,
|
id,
|
||||||
path,
|
path,
|
||||||
modified_epoch_secs,
|
modified_epoch_secs,
|
||||||
message_count,
|
message_count: stored.map_or(derived_message_count, |metadata| {
|
||||||
|
metadata.message_count as usize
|
||||||
|
}),
|
||||||
|
model: stored.map(|metadata| metadata.model.clone()),
|
||||||
|
started_at: stored.map(|metadata| metadata.started_at.clone()),
|
||||||
|
last_prompt: stored.and_then(|metadata| metadata.last_prompt.clone()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
sessions.sort_by(|left, right| right.modified_epoch_secs.cmp(&left.modified_epoch_secs));
|
sessions.sort_by(|left, right| right.modified_epoch_secs.cmp(&left.modified_epoch_secs));
|
||||||
@@ -1478,17 +1552,99 @@ fn render_session_list(active_session_id: &str) -> Result<String, Box<dyn std::e
|
|||||||
} else {
|
} else {
|
||||||
"○ saved"
|
"○ saved"
|
||||||
};
|
};
|
||||||
|
let model = session.model.as_deref().unwrap_or("unknown");
|
||||||
|
let started = session.started_at.as_deref().unwrap_or("unknown");
|
||||||
|
let last_prompt = session.last_prompt.as_deref().map_or_else(
|
||||||
|
|| "-".to_string(),
|
||||||
|
|prompt| truncate_for_summary(prompt, 36),
|
||||||
|
);
|
||||||
lines.push(format!(
|
lines.push(format!(
|
||||||
" {id:<20} {marker:<10} msgs={msgs:<4} modified={modified} path={path}",
|
" {id:<20} {marker:<10} msgs={msgs:<4} model={model:<24} started={started} modified={modified} last={last_prompt} path={path}",
|
||||||
id = session.id,
|
id = session.id,
|
||||||
msgs = session.message_count,
|
msgs = session.message_count,
|
||||||
|
model = model,
|
||||||
|
started = started,
|
||||||
modified = session.modified_epoch_secs,
|
modified = session.modified_epoch_secs,
|
||||||
|
last_prompt = last_prompt,
|
||||||
path = session.path.display(),
|
path = session.path.display(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(lines.join("\n"))
|
Ok(lines.join("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn current_epoch_secs() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.map(|duration| duration.as_secs())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_timestamp_rfc3339ish() -> String {
|
||||||
|
format!("{}Z", current_epoch_secs())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn last_prompt_from_session(session: &Session) -> Option<String> {
|
||||||
|
session
|
||||||
|
.messages
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.find(|message| message.role == MessageRole::User)
|
||||||
|
.and_then(|message| {
|
||||||
|
message.blocks.iter().find_map(|block| match block {
|
||||||
|
ContentBlock::Text { text } => Some(text.trim().to_string()),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.filter(|text| !text.is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn derive_session_metadata(session: &Session, model: &str) -> SessionMetadata {
|
||||||
|
let started_at = session
|
||||||
|
.metadata
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(current_timestamp_rfc3339ish, |metadata| {
|
||||||
|
metadata.started_at.clone()
|
||||||
|
});
|
||||||
|
SessionMetadata {
|
||||||
|
started_at,
|
||||||
|
model: model.to_string(),
|
||||||
|
message_count: session.messages.len().try_into().unwrap_or(u32::MAX),
|
||||||
|
last_prompt: last_prompt_from_session(session),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session_age_secs(modified_epoch_secs: u64) -> u64 {
|
||||||
|
current_epoch_secs().saturating_sub(modified_epoch_secs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn auto_compact_inactive_sessions(
|
||||||
|
active_session_id: &str,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
for summary in list_managed_sessions()? {
|
||||||
|
if summary.id == active_session_id
|
||||||
|
|| session_age_secs(summary.modified_epoch_secs) < OLD_SESSION_COMPACTION_AGE_SECS
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let path = summary.path.clone();
|
||||||
|
let Ok(session) = Session::load_from_path(&path) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if !runtime::should_compact(&session, CompactionConfig::default()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let mut compacted =
|
||||||
|
runtime::compact_session(&session, CompactionConfig::default()).compacted_session;
|
||||||
|
let model = compacted.metadata.as_ref().map_or_else(
|
||||||
|
|| DEFAULT_MODEL.to_string(),
|
||||||
|
|metadata| metadata.model.clone(),
|
||||||
|
);
|
||||||
|
compacted.metadata = Some(derive_session_metadata(&compacted, &model));
|
||||||
|
compacted.save_to_path(&path)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn render_repl_help() -> String {
|
fn render_repl_help() -> String {
|
||||||
[
|
[
|
||||||
"REPL".to_string(),
|
"REPL".to_string(),
|
||||||
@@ -1702,12 +1858,67 @@ fn render_memory_report() -> Result<String, Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
fn init_claude_md() -> Result<String, Box<dyn std::error::Error>> {
|
fn init_claude_md() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
let cwd = env::current_dir()?;
|
let cwd = env::current_dir()?;
|
||||||
Ok(initialize_repo(&cwd)?.render())
|
let claude_md = cwd.join("CLAUDE.md");
|
||||||
|
if claude_md.exists() {
|
||||||
|
return Ok(format_init_report(&claude_md, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
let content = render_init_claude_md(&cwd);
|
||||||
|
fs::write(&claude_md, content)?;
|
||||||
|
Ok(format_init_report(&claude_md, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_init() -> Result<(), Box<dyn std::error::Error>> {
|
fn render_init_claude_md(cwd: &Path) -> String {
|
||||||
println!("{}", init_claude_md()?);
|
let mut lines = vec![
|
||||||
Ok(())
|
"# CLAUDE.md".to_string(),
|
||||||
|
String::new(),
|
||||||
|
"This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.".to_string(),
|
||||||
|
String::new(),
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut command_lines = Vec::new();
|
||||||
|
if cwd.join("rust").join("Cargo.toml").is_file() {
|
||||||
|
command_lines.push("- Run Rust verification from `rust/`: `cargo fmt`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`".to_string());
|
||||||
|
} else if cwd.join("Cargo.toml").is_file() {
|
||||||
|
command_lines.push("- Run Rust verification from the repo root: `cargo fmt`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`".to_string());
|
||||||
|
}
|
||||||
|
if cwd.join("tests").is_dir() && cwd.join("src").is_dir() {
|
||||||
|
command_lines.push("- `src/` and `tests/` are also present; check those surfaces before removing or renaming Python-era compatibility assets.".to_string());
|
||||||
|
}
|
||||||
|
if !command_lines.is_empty() {
|
||||||
|
lines.push("## Verification".to_string());
|
||||||
|
lines.extend(command_lines);
|
||||||
|
lines.push(String::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut structure_lines = Vec::new();
|
||||||
|
if cwd.join("rust").is_dir() {
|
||||||
|
structure_lines.push(
|
||||||
|
"- `rust/` contains the Rust workspace and the active CLI/runtime implementation."
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if cwd.join("src").is_dir() {
|
||||||
|
structure_lines.push("- `src/` contains the older Python-first workspace artifacts referenced by the repo history and tests.".to_string());
|
||||||
|
}
|
||||||
|
if cwd.join("tests").is_dir() {
|
||||||
|
structure_lines.push("- `tests/` exercises compatibility and porting behavior across the repository surfaces.".to_string());
|
||||||
|
}
|
||||||
|
if !structure_lines.is_empty() {
|
||||||
|
lines.push("## Repository shape".to_string());
|
||||||
|
lines.extend(structure_lines);
|
||||||
|
lines.push(String::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push("## Working agreement".to_string());
|
||||||
|
lines.push("- Prefer small, reviewable Rust changes and keep slash-command behavior aligned between the shared command registry and the CLI entrypoints.".to_string());
|
||||||
|
lines.push("- Do not overwrite existing CLAUDE.md content automatically; update it intentionally when repo workflows change.".to_string());
|
||||||
|
lines.push(String::new());
|
||||||
|
|
||||||
|
lines.join(
|
||||||
|
"
|
||||||
|
",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_permission_mode(mode: &str) -> Option<&'static str> {
|
fn normalize_permission_mode(mode: &str) -> Option<&'static str> {
|
||||||
@@ -2266,65 +2477,34 @@ fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_help_to(out: &mut impl Write) -> io::Result<()> {
|
fn print_help() {
|
||||||
writeln!(out, "rusty-claude-cli v{VERSION}")?;
|
println!("rusty-claude-cli v{VERSION}");
|
||||||
writeln!(out)?;
|
println!();
|
||||||
writeln!(out, "Usage:")?;
|
println!("Usage:");
|
||||||
writeln!(
|
println!(" rusty-claude-cli [--model MODEL] [--allowedTools TOOL[,TOOL...]]");
|
||||||
out,
|
println!(" Start the interactive REPL");
|
||||||
" rusty-claude-cli [--model MODEL] [--allowedTools TOOL[,TOOL...]]"
|
println!(" rusty-claude-cli [--model MODEL] [--output-format text|json] prompt TEXT");
|
||||||
)?;
|
println!(" Send one prompt and exit");
|
||||||
writeln!(out, " Start the interactive REPL")?;
|
println!(" rusty-claude-cli [--model MODEL] [--output-format text|json] TEXT");
|
||||||
writeln!(
|
println!(" Shorthand non-interactive prompt mode");
|
||||||
out,
|
println!(" rusty-claude-cli --resume SESSION.json [/status] [/compact] [...]");
|
||||||
" rusty-claude-cli [--model MODEL] [--output-format text|json] prompt TEXT"
|
println!(" Inspect or maintain a saved session without entering the REPL");
|
||||||
)?;
|
println!(" rusty-claude-cli dump-manifests");
|
||||||
writeln!(out, " Send one prompt and exit")?;
|
println!(" rusty-claude-cli bootstrap-plan");
|
||||||
writeln!(
|
println!(" rusty-claude-cli system-prompt [--cwd PATH] [--date YYYY-MM-DD]");
|
||||||
out,
|
println!(" rusty-claude-cli login");
|
||||||
" rusty-claude-cli [--model MODEL] [--output-format text|json] TEXT"
|
println!(" rusty-claude-cli logout");
|
||||||
)?;
|
println!();
|
||||||
writeln!(out, " Shorthand non-interactive prompt mode")?;
|
println!("Flags:");
|
||||||
writeln!(
|
println!(" --model MODEL Override the active model");
|
||||||
out,
|
println!(" --output-format FORMAT Non-interactive output format: text or json");
|
||||||
" rusty-claude-cli --resume SESSION.json [/status] [/compact] [...]"
|
println!(" --permission-mode MODE Set read-only, workspace-write, or danger-full-access");
|
||||||
)?;
|
println!(" --allowedTools TOOLS Restrict enabled tools (repeatable; comma-separated aliases supported)");
|
||||||
writeln!(
|
println!(" --version, -V Print version and build information locally");
|
||||||
out,
|
println!();
|
||||||
" Inspect or maintain a saved session without entering the REPL"
|
println!("Interactive slash commands:");
|
||||||
)?;
|
println!("{}", render_slash_command_help());
|
||||||
writeln!(out, " rusty-claude-cli dump-manifests")?;
|
println!();
|
||||||
writeln!(out, " rusty-claude-cli bootstrap-plan")?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" rusty-claude-cli system-prompt [--cwd PATH] [--date YYYY-MM-DD]"
|
|
||||||
)?;
|
|
||||||
writeln!(out, " rusty-claude-cli login")?;
|
|
||||||
writeln!(out, " rusty-claude-cli logout")?;
|
|
||||||
writeln!(out, " rusty-claude-cli init")?;
|
|
||||||
writeln!(out)?;
|
|
||||||
writeln!(out, "Flags:")?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" --model MODEL Override the active model"
|
|
||||||
)?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" --output-format FORMAT Non-interactive output format: text or json"
|
|
||||||
)?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" --permission-mode MODE Set read-only, workspace-write, or danger-full-access"
|
|
||||||
)?;
|
|
||||||
writeln!(out, " --allowedTools TOOLS Restrict enabled tools (repeatable; comma-separated aliases supported)")?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" --version, -V Print version and build information locally"
|
|
||||||
)?;
|
|
||||||
writeln!(out)?;
|
|
||||||
writeln!(out, "Interactive slash commands:")?;
|
|
||||||
writeln!(out, "{}", render_slash_command_help())?;
|
|
||||||
writeln!(out)?;
|
|
||||||
let resume_commands = resume_supported_slash_commands()
|
let resume_commands = resume_supported_slash_commands()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|spec| match spec.argument_hint {
|
.map(|spec| match spec.argument_hint {
|
||||||
@@ -2333,46 +2513,84 @@ fn print_help_to(out: &mut impl Write) -> io::Result<()> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
writeln!(out, "Resume-safe commands: {resume_commands}")?;
|
println!("Resume-safe commands: {resume_commands}");
|
||||||
writeln!(out, "Examples:")?;
|
println!("Examples:");
|
||||||
writeln!(
|
println!(" rusty-claude-cli --model claude-opus \"summarize this repo\"");
|
||||||
out,
|
println!(" rusty-claude-cli --output-format json prompt \"explain src/main.rs\"");
|
||||||
" rusty-claude-cli --model claude-opus \"summarize this repo\""
|
println!(" rusty-claude-cli --allowedTools read,glob \"summarize Cargo.toml\"");
|
||||||
)?;
|
println!(" rusty-claude-cli --resume session.json /status /diff /export notes.txt");
|
||||||
writeln!(
|
println!(" rusty-claude-cli login");
|
||||||
out,
|
|
||||||
" rusty-claude-cli --output-format json prompt \"explain src/main.rs\""
|
|
||||||
)?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" rusty-claude-cli --allowedTools read,glob \"summarize Cargo.toml\""
|
|
||||||
)?;
|
|
||||||
writeln!(
|
|
||||||
out,
|
|
||||||
" rusty-claude-cli --resume session.json /status /diff /export notes.txt"
|
|
||||||
)?;
|
|
||||||
writeln!(out, " rusty-claude-cli login")?;
|
|
||||||
writeln!(out, " rusty-claude-cli init")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_help() {
|
|
||||||
let _ = print_help_to(&mut io::stdout());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{
|
use super::{
|
||||||
filter_tool_specs, format_compact_report, format_cost_report, format_model_report,
|
derive_session_metadata, filter_tool_specs, format_compact_report, format_cost_report,
|
||||||
format_model_switch_report, format_permissions_report, format_permissions_switch_report,
|
format_init_report, format_model_report, format_model_switch_report,
|
||||||
format_resume_report, format_status_report, format_tool_call_start, format_tool_result,
|
format_permissions_report, format_permissions_switch_report, format_resume_report,
|
||||||
normalize_permission_mode, parse_args, parse_git_status_metadata, print_help_to,
|
format_status_report, format_tool_call_start, format_tool_result, list_managed_sessions,
|
||||||
render_config_report, render_memory_report, render_repl_help,
|
normalize_permission_mode, parse_args, parse_git_status_metadata, render_config_report,
|
||||||
resume_supported_slash_commands, status_context, CliAction, CliOutputFormat, SlashCommand,
|
render_init_claude_md, render_memory_report, render_repl_help,
|
||||||
StatusUsage, DEFAULT_MODEL,
|
resume_supported_slash_commands, sessions_dir, status_context, CliAction, CliOutputFormat,
|
||||||
|
SlashCommand, StatusUsage, DEFAULT_MODEL,
|
||||||
};
|
};
|
||||||
use runtime::{ContentBlock, ConversationMessage, MessageRole, PermissionMode};
|
use runtime::{ContentBlock, ConversationMessage, MessageRole, PermissionMode, Session};
|
||||||
use std::path::PathBuf;
|
use std::fs;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn derive_session_metadata_recomputes_prompt_and_count() {
|
||||||
|
let mut session = Session::new();
|
||||||
|
session
|
||||||
|
.messages
|
||||||
|
.push(ConversationMessage::user_text("first prompt"));
|
||||||
|
session
|
||||||
|
.messages
|
||||||
|
.push(ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||||
|
text: "reply".to_string(),
|
||||||
|
}]));
|
||||||
|
let metadata = derive_session_metadata(&session, "claude-test");
|
||||||
|
assert_eq!(metadata.model, "claude-test");
|
||||||
|
assert_eq!(metadata.message_count, 2);
|
||||||
|
assert_eq!(metadata.last_prompt.as_deref(), Some("first prompt"));
|
||||||
|
assert!(metadata.started_at.ends_with('Z'));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn managed_sessions_use_home_directory_and_list_metadata() {
|
||||||
|
let temp =
|
||||||
|
std::env::temp_dir().join(format!("rusty-claude-cli-home-{}", std::process::id()));
|
||||||
|
let _ = fs::remove_dir_all(&temp);
|
||||||
|
fs::create_dir_all(&temp).expect("temp home should exist");
|
||||||
|
let previous_home = std::env::var_os("HOME");
|
||||||
|
std::env::set_var("HOME", &temp);
|
||||||
|
|
||||||
|
let dir = sessions_dir().expect("sessions dir");
|
||||||
|
assert_eq!(dir, temp.join(".claude").join("sessions"));
|
||||||
|
|
||||||
|
let mut session = Session::new();
|
||||||
|
session
|
||||||
|
.messages
|
||||||
|
.push(ConversationMessage::user_text("persist me"));
|
||||||
|
session.metadata = Some(derive_session_metadata(&session, "claude-home"));
|
||||||
|
let file = dir.join("session-test.json");
|
||||||
|
session.save_to_path(&file).expect("session save");
|
||||||
|
|
||||||
|
let listed = list_managed_sessions().expect("session list");
|
||||||
|
let found = listed
|
||||||
|
.into_iter()
|
||||||
|
.find(|entry| entry.id == "session-test")
|
||||||
|
.expect("saved session should be listed");
|
||||||
|
assert_eq!(found.message_count, 1);
|
||||||
|
assert_eq!(found.model.as_deref(), Some("claude-home"));
|
||||||
|
assert_eq!(found.last_prompt.as_deref(), Some("persist me"));
|
||||||
|
|
||||||
|
fs::remove_file(file).ok();
|
||||||
|
if let Some(previous_home) = previous_home {
|
||||||
|
std::env::set_var("HOME", previous_home);
|
||||||
|
}
|
||||||
|
fs::remove_dir_all(temp).ok();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn defaults_to_repl_when_no_args() {
|
fn defaults_to_repl_when_no_args() {
|
||||||
@@ -2508,10 +2726,6 @@ mod tests {
|
|||||||
parse_args(&["logout".to_string()]).expect("logout should parse"),
|
parse_args(&["logout".to_string()]).expect("logout should parse"),
|
||||||
CliAction::Logout
|
CliAction::Logout
|
||||||
);
|
);
|
||||||
assert_eq!(
|
|
||||||
parse_args(&["init".to_string()]).expect("init should parse"),
|
|
||||||
CliAction::Init
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -2583,7 +2797,8 @@ mod tests {
|
|||||||
assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
|
assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
|
||||||
assert!(help.contains("/clear [--confirm]"));
|
assert!(help.contains("/clear [--confirm]"));
|
||||||
assert!(help.contains("/cost"));
|
assert!(help.contains("/cost"));
|
||||||
assert!(help.contains("/resume <session-path>"));
|
assert!(help.contains("/resume <session-id-or-path>"));
|
||||||
|
assert!(help.contains("/sessions"));
|
||||||
assert!(help.contains("/config [env|hooks|model]"));
|
assert!(help.contains("/config [env|hooks|model]"));
|
||||||
assert!(help.contains("/memory"));
|
assert!(help.contains("/memory"));
|
||||||
assert!(help.contains("/init"));
|
assert!(help.contains("/init"));
|
||||||
@@ -2666,11 +2881,12 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn init_help_mentions_direct_subcommand() {
|
fn init_report_uses_structured_output() {
|
||||||
let mut help = Vec::new();
|
let created = format_init_report(Path::new("/tmp/CLAUDE.md"), true);
|
||||||
print_help_to(&mut help).expect("help should render");
|
assert!(created.contains("Init"));
|
||||||
let help = String::from_utf8(help).expect("help should be utf8");
|
assert!(created.contains("Result created"));
|
||||||
assert!(help.contains("rusty-claude-cli init"));
|
let skipped = format_init_report(Path::new("/tmp/CLAUDE.md"), false);
|
||||||
|
assert!(skipped.contains("skipped (already exists)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -2774,7 +2990,7 @@ mod tests {
|
|||||||
fn status_context_reads_real_workspace_metadata() {
|
fn status_context_reads_real_workspace_metadata() {
|
||||||
let context = status_context(None).expect("status context should load");
|
let context = status_context(None).expect("status context should load");
|
||||||
assert!(context.cwd.is_absolute());
|
assert!(context.cwd.is_absolute());
|
||||||
assert_eq!(context.discovered_config_files, 5);
|
assert!(context.discovered_config_files >= 3);
|
||||||
assert!(context.loaded_config_files <= context.discovered_config_files);
|
assert!(context.loaded_config_files <= context.discovered_config_files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2832,7 +3048,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn init_template_mentions_detected_rust_workspace() {
|
fn init_template_mentions_detected_rust_workspace() {
|
||||||
let rendered = crate::init::render_init_claude_md(std::path::Path::new("."));
|
let rendered = render_init_claude_md(Path::new("."));
|
||||||
assert!(rendered.contains("# CLAUDE.md"));
|
assert!(rendered.contains("# CLAUDE.md"));
|
||||||
assert!(rendered.contains("cargo clippy --workspace --all-targets -- -D warnings"));
|
assert!(rendered.contains("cargo clippy --workspace --all-targets -- -D warnings"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user