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:
1 parent
0123f41d99
commit
0568b92e06
9 files changed
+556
-100
No files matched your search
+102
-17
@@ -301,25 +301,15 @@ async fn agent_mode_streams_events_and_persists() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn first_message_auto_titles_and_rename_delete_work() {
|
||||
async fn name_is_generated_editable_then_locked() {
|
||||
let (app, store) = build_app().await;
|
||||
let id = create_session(&app).await;
|
||||
assert_eq!(store.get_session(&id).unwrap().unwrap().title, "Nuova chat");
|
||||
let sess = store.get_session(&id).unwrap().unwrap();
|
||||
// Framework-assigned friendly name (not the old "Nuova chat"), unlocked.
|
||||
assert!(sess.title.contains('-'), "title: {}", sess.title);
|
||||
assert!(!sess.name_locked);
|
||||
|
||||
// First user message auto-titles the session.
|
||||
req(
|
||||
&app,
|
||||
Method::POST,
|
||||
&format!("/api/sessions/{id}/chat"),
|
||||
Some(serde_json::json!({"content": "Qual è la capitale?"})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
store.get_session(&id).unwrap().unwrap().title,
|
||||
"Qual è la capitale?"
|
||||
);
|
||||
|
||||
// Rename via PATCH.
|
||||
// Rename before the first interaction is allowed.
|
||||
let (st, _) = req(
|
||||
&app,
|
||||
Method::PATCH,
|
||||
@@ -330,8 +320,103 @@ async fn first_message_auto_titles_and_rename_delete_work() {
|
||||
assert_eq!(st, StatusCode::OK);
|
||||
assert_eq!(store.get_session(&id).unwrap().unwrap().title, "Mio titolo");
|
||||
|
||||
// Delete via DELETE.
|
||||
// The first interaction locks the name.
|
||||
req(
|
||||
&app,
|
||||
Method::POST,
|
||||
&format!("/api/sessions/{id}/chat"),
|
||||
Some(serde_json::json!({"content": "ciao"})),
|
||||
)
|
||||
.await;
|
||||
assert!(store.get_session(&id).unwrap().unwrap().name_locked);
|
||||
|
||||
// Rename after the first interaction is rejected; the name is unchanged.
|
||||
let (st, _) = req(
|
||||
&app,
|
||||
Method::PATCH,
|
||||
&format!("/api/sessions/{id}"),
|
||||
Some(serde_json::json!({"title": "Troppo tardi"})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(st, StatusCode::BAD_REQUEST);
|
||||
assert_eq!(store.get_session(&id).unwrap().unwrap().title, "Mio titolo");
|
||||
|
||||
// Delete still works.
|
||||
let (st, _) = req(&app, Method::DELETE, &format!("/api/sessions/{id}"), None).await;
|
||||
assert_eq!(st, StatusCode::NO_CONTENT);
|
||||
assert!(store.get_session(&id).unwrap().is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn settings_are_copied_on_create_and_isolated() {
|
||||
let (app, store) = build_app().await;
|
||||
// Seed the global defaults template.
|
||||
let (st, _) = req(
|
||||
&app,
|
||||
Method::PUT,
|
||||
"/api/settings/defaults",
|
||||
Some(serde_json::json!({"system_prompt": "base", "temperature": 0.9})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(st, StatusCode::OK);
|
||||
|
||||
// A new session snapshots the defaults.
|
||||
let (st, text) = req(
|
||||
&app,
|
||||
Method::POST,
|
||||
"/api/sessions",
|
||||
Some(serde_json::json!({})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(st, StatusCode::OK, "{text}");
|
||||
let v: serde_json::Value = serde_json::from_str(&text).unwrap();
|
||||
let id = v["id"].as_str().unwrap().to_string();
|
||||
assert_eq!(v["settings"]["system_prompt"], "base");
|
||||
assert_eq!(v["settings"]["temperature"], 0.9);
|
||||
|
||||
// Editing this session's settings affects only it.
|
||||
let (st, _) = req(
|
||||
&app,
|
||||
Method::PATCH,
|
||||
&format!("/api/sessions/{id}"),
|
||||
Some(serde_json::json!({"settings": {"notes": "scopo: test", "temperature": 0.2}})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(st, StatusCode::OK);
|
||||
let s = store.get_session(&id).unwrap().unwrap();
|
||||
assert_eq!(s.settings.notes.as_deref(), Some("scopo: test"));
|
||||
assert_eq!(s.settings.temperature, Some(0.2));
|
||||
// The global defaults are untouched.
|
||||
assert_eq!(
|
||||
store.get_defaults().unwrap().unwrap().temperature,
|
||||
Some(0.9)
|
||||
);
|
||||
|
||||
// Changing the globals affects only FUTURE sessions, not the existing one.
|
||||
req(
|
||||
&app,
|
||||
Method::PUT,
|
||||
"/api/settings/defaults",
|
||||
Some(serde_json::json!({"temperature": 0.5})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
store
|
||||
.get_session(&id)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.settings
|
||||
.temperature,
|
||||
Some(0.2)
|
||||
);
|
||||
let id2 = create_session(&app).await;
|
||||
assert_eq!(
|
||||
store
|
||||
.get_session(&id2)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.settings
|
||||
.temperature,
|
||||
Some(0.5)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user