Files
strata-ctl/src/name.rs
T
adm 0568b92e06 Session settings copy-on-create + immutable friendly names
- SessionSettings instance per session (system_prompt, notes, sampling), snapshotted from global defaults at creation (copy-on-create); editing a session affects only it, editing globals affects only future sessions. Global defaults template stored in DB, seeded from config; GET/PUT /api/settings/defaults.

- Framework-assigned evocative names (word list, src/name.rs) replace the auto-title (removed). Name editable only before the first interaction, then locked (name_locked); PATCH rejects post-lock rename. Session notes carry the user-defined purpose.

- Idempotent migration adds settings_json/name_locked and backfills old sessions; build_request uses the session's own settings snapshot. 112 tests green, clippy/fmt clean; verified live (migration, name lock, settings isolation).
2026-11-25 15:44:52 +01:00

100 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Framework-assigned session names: memorable, evocative identifiers (a curated
//! word list, adjective–noun) rather than anonymous hex or sequential numbers.
//! The name is the session's identity: user-editable only before the first
//! interaction, immutable afterwards, and it names the personal folder. The
//! descriptive "purpose" lives in the session notes instead, not in the name.
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
// Masculine nouns + masculine/invariant adjectives so the combinations agree.
const NOUN: &[&str] = &[
"lupo", "fiume", "bosco", "vento", "mare", "falco", "sasso", "ramo", "fuoco", "gelso", "pino",
"scoglio", "sentiero", "prato", "lago", "monte", "colle", "porto", "faro", "ponte",
];
const ADJ: &[&str] = &[
"rosso", "dorato", "quieto", "antico", "veloce", "luminoso", "ombroso", "felice", "saggio",
"ribelle", "solare", "notturno", "fermo", "vivace", "placido", "rapido", "saldo", "gentile",
"fiero", "curioso",
];
/// A pseudo-random index without a `rand` dependency (nanos + counter mixed).
fn pick(len: usize) -> usize {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let mixed = (nanos as u64).wrapping_mul(31).wrapping_add(n);
(mixed as usize) % len
}
/// Generate a friendly name not already present in `taken`. On collision a
/// numeric suffix is appended (`lupo-rosso-2`, `-3`, …).
pub fn friendly_name(taken: &[String]) -> String {
let base = format!("{}-{}", NOUN[pick(NOUN.len())], ADJ[pick(ADJ.len())]);
if !taken.iter().any(|t| t == &base) {
return base;
}
let mut n = 2;
loop {
let candidate = format!("{base}-{n}");
if !taken.iter().any(|t| t == &candidate) {
return candidate;
}
n += 1;
}
}
/// A filesystem-safe slug derived from a name: lowercase ASCII alphanumerics,
/// every other run collapsed to a single `-`, trimmed. Used for folder names.
pub fn slug(name: &str) -> String {
let mut out = String::new();
let mut dash = false;
for c in name.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
dash = false;
} else if !dash && !out.is_empty() {
out.push('-');
dash = true;
}
}
while out.ends_with('-') {
out.pop();
}
if out.is_empty() {
out.push_str("sessione");
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_is_friendly_and_unique() {
let mut taken: Vec<String> = vec![];
for _ in 0..60 {
let n = friendly_name(&taken);
assert!(n.contains('-'));
assert!(n
.chars()
.all(|c| c.is_ascii_lowercase() || c == '-' || c.is_ascii_digit()));
assert!(!taken.contains(&n), "duplicate name {n}");
taken.push(n);
}
}
#[test]
fn slug_is_filesystem_safe() {
assert_eq!(slug("Lupo Rosso"), "lupo-rosso");
assert_eq!(slug(" Ciao!! Mondo "), "ciao-mondo");
assert_eq!(slug("a/b\\c"), "a-b-c");
assert_eq!(slug("---"), "sessione");
assert_eq!(slug("lupo-rosso"), "lupo-rosso");
}
}