Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

refactor(llm): drop cognomen and hand-write enum wire labels

Matheus Cardoso569abea11d0a
diff --git a/Cargo.lock b/Cargo.lock
index bf138494..f53710ed 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -166,7 +166,6 @@ dependencies = [
"anyhow",
"async-trait",
"base64",
- "cognomen",
"jiff",
"regex",
"reqwest 0.12.28",
@@ -652,27 +651,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
-[[package]]
-name = "cognomen"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e31c96aac453db73ae5d52665320b902889e596d64cb6a1212b66762367ca857"
-dependencies = [
- "cognomen-macros",
- "serde",
-]
-
-[[package]]
-name = "cognomen-macros"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "956eeb02b6e4e9c30f26500b8f933b15c5bbf3f0331cedae5f64671bbb394922"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
[[package]]
name = "colorchoice"
version = "1.0.5"
diff --git a/Cargo.toml b/Cargo.toml
index 0316d548..2ca873ff 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -44,9 +44,6 @@ thiserror = "2"
# Serialization.
serde = { version = "1", features = ["derive"] }
-# Compile-time enum labels for wire/config strings. `serde` generates
-# Serialize/Deserialize from the same labels as `Label::as_str`.
-cognomen = { version = "0.5", features = ["serde"] }
# `preserve_order` keeps existing JSON key order intact when we
# round-trip user config files via `install-mcp --apply`. Without it,
# `~/.claude.json` (which has dozens of CC-managed keys) would be
diff --git a/crates/ai-memory-llm/Cargo.toml b/crates/ai-memory-llm/Cargo.toml
index 275742d7..3569f340 100644
--- a/crates/ai-memory-llm/Cargo.toml
+++ b/crates/ai-memory-llm/Cargo.toml
@@ -14,7 +14,6 @@ anyhow.workspace = true
thiserror.workspace = true
serde.workspace = true
serde_json.workspace = true
-cognomen.workspace = true
schemars.workspace = true
secrecy.workspace = true
tokio.workspace = true
diff --git a/crates/ai-memory-llm/src/anthropic.rs b/crates/ai-memory-llm/src/anthropic.rs
index 342b3a43..1b2b2009 100644
--- a/crates/ai-memory-llm/src/anthropic.rs
+++ b/crates/ai-memory-llm/src/anthropic.rs
@@ -3,7 +3,6 @@
use std::time::Duration;
use async_trait::async_trait;
-use cognomen::Cognomen;
use secrecy::{ExposeSecret, SecretString};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
@@ -249,7 +248,7 @@ impl AnthropicProvider {
.messages
.iter()
.map(|m| AnthropicMsg {
- role: m.role.as_ref(),
+ role: m.role.as_str(),
content: &m.content,
})
.collect();
@@ -344,8 +343,7 @@ enum ClaudeId {
MythosPreview,
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Cognomen)]
-#[cognomen(lower)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ClaudeFamily {
Opus,
Sonnet,
@@ -355,6 +353,16 @@ enum ClaudeFamily {
}
impl ClaudeFamily {
+ const fn as_str(self) -> &'static str {
+ match self {
+ Self::Opus => "opus",
+ Self::Sonnet => "sonnet",
+ Self::Haiku => "haiku",
+ Self::Fable => "fable",
+ Self::Mythos => "mythos",
+ }
+ }
+
fn effort_since(self) -> Option<(u32, u32)> {
match self {
Self::Opus => Some((4, 5)),
@@ -373,6 +381,25 @@ impl ClaudeFamily {
}
}
+impl std::str::FromStr for ClaudeFamily {
+ type Err = ();
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ for family in [
+ Self::Opus,
+ Self::Sonnet,
+ Self::Haiku,
+ Self::Fable,
+ Self::Mythos,
+ ] {
+ if s == family.as_str() {
+ return Ok(family);
+ }
+ }
+ Err(())
+ }
+}
+
impl ClaudeId {
fn parse(model: &str) -> Option<Self> {
let lower = model.to_ascii_lowercase();
@@ -685,6 +712,20 @@ mod tests {
}
}
+ #[test]
+ fn claude_family_parses_lowercase_labels() {
+ for family in [
+ ClaudeFamily::Opus,
+ ClaudeFamily::Sonnet,
+ ClaudeFamily::Haiku,
+ ClaudeFamily::Fable,
+ ClaudeFamily::Mythos,
+ ] {
+ assert_eq!(family.as_str().parse::<ClaudeFamily>().unwrap(), family);
+ }
+ assert!("unknown".parse::<ClaudeFamily>().is_err());
+ }
+
#[test]
fn with_base_url_is_preserved_after_oauth_construction() {
let provider = AnthropicProvider::new_oauth(SecretString::from("tok"), "claude-sonnet-4-6")
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index 797e270f..ee58ba42 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -409,7 +409,7 @@ fn build_chat_request<'a>(
}
for m in &request.messages {
messages.push(CopilotMsg {
- role: m.role.as_ref(),
+ role: m.role.as_str(),
content: &m.content,
});
}
diff --git a/crates/ai-memory-llm/src/health.rs b/crates/ai-memory-llm/src/health.rs
index 03e16dd5..0860da57 100644
--- a/crates/ai-memory-llm/src/health.rs
+++ b/crates/ai-memory-llm/src/health.rs
@@ -6,7 +6,6 @@
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
-use cognomen::Cognomen;
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
@@ -64,8 +63,8 @@ impl ProviderHealth {
}
/// Wire-format provider-health status.
-#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Cognomen)]
-#[cognomen(kebab)]
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "kebab-case")]
pub enum ProviderHealthStatus {
/// The role has no provider configured.
#[default]
@@ -78,6 +77,19 @@ pub enum ProviderHealthStatus {
Error,
}
+impl ProviderHealthStatus {
+ /// Canonical kebab-case wire string.
+ #[must_use]
+ pub const fn as_str(&self) -> &'static str {
+ match self {
+ Self::Disabled => "disabled",
+ Self::Unknown => "unknown",
+ Self::Ok => "ok",
+ Self::Error => "error",
+ }
+ }
+}
+
/// Wire-format health snapshot for all provider roles.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ProviderHealthSnapshot {
@@ -491,4 +503,24 @@ mod tests {
assert_eq!(after.status, ProviderHealthStatus::Ok);
assert!(after.last_call_at.is_some());
}
+
+ #[test]
+ fn provider_health_status_wire_labels() {
+ for (status, label) in [
+ (ProviderHealthStatus::Disabled, "disabled"),
+ (ProviderHealthStatus::Unknown, "unknown"),
+ (ProviderHealthStatus::Ok, "ok"),
+ (ProviderHealthStatus::Error, "error"),
+ ] {
+ assert_eq!(status.as_str(), label);
+ assert_eq!(
+ serde_json::to_value(status).unwrap(),
+ serde_json::json!(label)
+ );
+ assert_eq!(
+ serde_json::from_value::<ProviderHealthStatus>(serde_json::json!(label)).unwrap(),
+ status
+ );
+ }
+ }
}
diff --git a/crates/ai-memory-llm/src/openai.rs b/crates/ai-memory-llm/src/openai.rs
index fdf532aa..7b951bf5 100644
--- a/crates/ai-memory-llm/src/openai.rs
+++ b/crates/ai-memory-llm/src/openai.rs
@@ -285,7 +285,7 @@ impl OpenAiProvider {
}
for m in &request.messages {
messages.push(OpenAiMsg {
- role: m.role.as_ref(),
+ role: m.role.as_str(),
content: &m.content,
});
}
diff --git a/crates/ai-memory-llm/src/openai_oauth.rs b/crates/ai-memory-llm/src/openai_oauth.rs
index 1b44bf11..1973512f 100644
--- a/crates/ai-memory-llm/src/openai_oauth.rs
+++ b/crates/ai-memory-llm/src/openai_oauth.rs
@@ -318,7 +318,7 @@ fn build_request<'a>(
.messages
.iter()
.map(|msg| CodexInputMessage {
- role: msg.role.as_ref(),
+ role: msg.role.as_str(),
content: vec![CodexInputContent {
kind: "input_text",
text: &msg.content,
diff --git a/crates/ai-memory-llm/src/types.rs b/crates/ai-memory-llm/src/types.rs
index ca53fd28..bb7e839b 100644
--- a/crates/ai-memory-llm/src/types.rs
+++ b/crates/ai-memory-llm/src/types.rs
@@ -1,11 +1,10 @@
//! Provider-neutral request/response types.
-use cognomen::{Cognomen, Label};
use serde::{Deserialize, Serialize};
/// Message role in a chat turn.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Cognomen)]
-#[cognomen(lower)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
pub enum Role {
/// Message from the user.
User,
@@ -13,6 +12,17 @@ pub enum Role {
Assistant,
}
+impl Role {
+ /// Canonical lowercase wire string (`user` / `assistant`).
+ #[must_use]
+ pub const fn as_str(&self) -> &'static str {
+ match self {
+ Self::User => "user",
+ Self::Assistant => "assistant",
+ }
+ }
+}
+
/// One message in the chat history.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
@@ -116,8 +126,8 @@ pub struct ChatResponse {
/// Omitting the config key (Rust `None`) leaves the model default;
/// [`Self::None`] is the wire value `none` (disable reasoning where the
/// backend allows it).
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Cognomen)]
-#[cognomen(lower)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
pub enum ReasoningEffort {
/// Disable reasoning (`none`).
None,
@@ -140,10 +150,20 @@ pub enum ReasoningEffort {
}
impl ReasoningEffort {
- /// Canonical wire-format string.
+ /// Canonical lowercase wire-format string.
#[must_use]
- pub fn as_str(self) -> &'static str {
- Label::as_str(&self)
+ pub const fn as_str(&self) -> &'static str {
+ match self {
+ Self::None => "none",
+ Self::Minimal => "minimal",
+ Self::Low => "low",
+ Self::Medium => "medium",
+ Self::High => "high",
+ Self::XHigh => "xhigh",
+ Self::Max => "max",
+ Self::Ultra => "ultra",
+ Self::Persistent => "persistent",
+ }
}
/// xAI Grok Chat Completions only accepts `low`/`medium`/`high`/`xhigh`
@@ -184,14 +204,25 @@ impl ReasoningEffort {
#[cfg(test)]
mod tests {
use super::ReasoningEffort;
- use cognomen::Variants;
use rstest::rstest;
+ const ALL_EFFORTS: [ReasoningEffort; 9] = [
+ ReasoningEffort::None,
+ ReasoningEffort::Minimal,
+ ReasoningEffort::Low,
+ ReasoningEffort::Medium,
+ ReasoningEffort::High,
+ ReasoningEffort::XHigh,
+ ReasoningEffort::Max,
+ ReasoningEffort::Ultra,
+ ReasoningEffort::Persistent,
+ ];
+
#[test]
fn reasoning_effort_roundtrips() {
assert_eq!(
- ReasoningEffort::LABELS,
- &[
+ ALL_EFFORTS.map(|effort| effort.as_str()),
+ [
"none",
"minimal",
"low",
@@ -203,11 +234,10 @@ mod tests {
"persistent",
]
);
- for &effort in ReasoningEffort::VARIANTS {
+ for effort in ALL_EFFORTS {
let raw = effort.as_str();
let parsed: ReasoningEffort = serde_json::from_value(serde_json::json!(raw)).unwrap();
assert_eq!(parsed, effort);
- assert_eq!(effort, raw);
assert_eq!(
serde_json::to_value(effort).unwrap(),
serde_json::json!(raw)