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).
This commit is contained in:
adm committed 2026-11-25 15:44:52 +01:00
1 parent 0123f41d99
commit 0568b92e06
9 files changed
+556 -100

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
//! 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");
}
}