1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! A loaded session's config (#1720): the one it opened with, which each
//! turn first brings up to date with the global settings that apply at a
//! loaded session's next turn: the advisor, automatic compaction, and the
//! codemode mode, with the configured providers and the title and
//! compaction models that resolve on them. A permission-mode change takes
//! the permission classifier's models the same way.
//!
//! Lazy on purpose: an idle session pays nothing for a change, and the turn
//! already holds the session's runtime to apply it.
use std::sync::{Arc, Mutex, PoisonError};
use super::LoadedSession;
use crate::{
agent::PERMISSION_CLASSIFIER_AGENT_ID,
app::session_runtime::SessionRuntime,
config::Config,
permission_classifier::DECISION_SCREEN_ID,
tools::advisor::{advisor_available, advisor_model},
};
/// What a loaded session's turns read.
pub(super) struct SessionConfig {
config: Arc<Config>,
/// The global config the session last took its next-turn settings
/// from, as the settings store published it.
applied: Arc<Config>,
}
impl SessionConfig {
/// `config`, opened from the global config `applied`.
pub(super) fn new(config: Config, applied: Arc<Config>) -> Mutex<Self> {
Mutex::new(Self {
config: Arc::new(config),
applied,
})
}
}
impl LoadedSession {
/// The config the session's turns read now.
pub(super) fn config(&self) -> Arc<Config> {
Arc::clone(&self.lock_config().config)
}
/// The config the turn about to start on `runtime` reads: the session's,
/// with the next-turn settings of the global config as it is now,
/// applied to `runtime` when they changed since the session's last
/// turn. Tells the session's clients what the model was told, and what
/// could not be applied, which leaves the session as it was until a
/// later turn applies it.
pub(super) async fn config_for_turn(&self, runtime: &mut SessionRuntime) -> Arc<Config> {
let (current, applied) = {
let config = self.lock_config();
(Arc::clone(&config.config), Arc::clone(&config.applied))
};
// No config loaded yet: the session keeps the one it opened with.
let Ok(global) = self.settings.agent_config().await else {
return current;
};
if Arc::ptr_eq(&global, &applied) {
return current;
}
let mut next = (*current).clone();
// Only what changed globally: a re-read that changed nothing, or
// changed other settings, leaves the agent's own values alone.
let changes = next.take_next_turn_settings(&applied, &global);
let mut notices = Vec::new();
let mut failed = false;
// Compaction first: the advisor's rebuild reads `next`, which must
// hold only compaction settings the session took.
if changes.compaction {
if let Err(error) = runtime.set_compaction(&next) {
failed = true;
notices.push(format!("could not apply the compaction settings: {error}"));
next.take_compaction_settings(¤t);
}
}
if changes.advisor {
let model = advisor_available(&next)
.then(|| advisor_model(&next).cloned())
.flatten();
match runtime.set_advisor(model, &next).await {
Ok(notice) => notices.extend(notice),
Err(error) => {
failed = true;
notices.push(format!("could not apply the advisor settings: {error}"));
next.take_advisor_settings(¤t);
}
}
}
if changes.codemode {
match runtime.set_codemode_mode(next.codemode.mode) {
Ok(notice) => notices.extend(notice),
Err(error) => {
failed = true;
notices.push(format!("could not apply the codemode mode: {error:#}"));
next.codemode.mode = current.codemode.mode;
}
}
}
if !notices.is_empty() {
self.feed.publish(
notices
.into_iter()
.map(|text| rho_protocol::SessionEvent::Notice { text }),
);
}
let next = Arc::new(next);
*self.lock_config() = SessionConfig {
config: Arc::clone(&next),
// A change that failed is tried again at the next turn; the
// flags only name what still differs from `global`.
applied: if failed { applied } else { global },
};
next
}
/// The config a permission-mode change reads: the session's, with the
/// permission classifier's models of the global config as it is now. A
/// classifier picked after the session opened then runs when a client
/// sets Auto; [`Self::keep_config`] keeps it once the mode applied.
pub(super) async fn config_for_permission_change(&self) -> Arc<Config> {
let Ok(global) = self.settings.agent_config().await else {
return self.config();
};
let mut next = (*self.config()).clone();
for id in [PERMISSION_CLASSIFIER_AGENT_ID, DECISION_SCREEN_ID] {
next.take_internal_agent(&global, id);
}
Arc::new(next)
}
/// Makes `config` the one the session's turns and rebuilds read.
pub(super) fn keep_config(&self, config: Arc<Config>) {
self.lock_config().config = config;
}
/// Nothing panics while holding the lock, so a poisoned one is still
/// consistent.
fn lock_config(&self) -> std::sync::MutexGuard<'_, SessionConfig> {
self.config.lock().unwrap_or_else(PoisonError::into_inner)
}
}