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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! In-memory startup configuration (issue #73): daemon startup without local
//! config files.
//!
//! Before #73, `thewayd` read `<base>/config.toml` at startup (through
//! local config files for the default model, the enabled builtin
//! skills, the trigger poll interval, the TUI feed scrollback cap, and the
//! orchestrator thinking-summary settings. Startup no longer touches local
//! config files: every value lives in [`StartupConfig`], seeded from the
//! built-in defaults and supplied through the settings RPC surface
//! (issue #72, [`WireDaemonConfig`]) — either as a controller-provided
//! initial payload at launch or as runtime `Configure` / `SetConfig`
//! updates.
//!
//! Runtime updates land through `TurnHost::handle_configure` (the serialized
//! event loop applies them authoritatively); this module covers the startup
//! side: defaults + optional initial payload + CLI overrides applied by the
//! composition root (`thewayd`).
use theway_core::ThinkingLevel;
use theway_transport::config::{ModelDefault, ThinkingSummarySettings};
use theway_transport::triggers::DEFAULT_DYNAMIC_TRIGGER_POLL_INTERVAL_SECS;
use theway_transport::wire::WireDaemonConfig;
/// Resolved startup settings (issue #73).
///
/// Every setting the daemon used to read from `config.toml` at startup,
/// held in memory: built-in defaults merged with the controller's initial
/// settings payload ([`WireDaemonConfig`]). CLI flags still win — the
/// composition root applies them after construction (same precedence as
/// before: CLI > settings > built-in default).
#[derive(Clone, Debug, PartialEq)]
pub struct StartupConfig {
/// Default provider/model pair, applied when the CLI specifies neither
/// `--provider` nor `--model` (`None` → env auto-detection, unchanged).
pub model_default: Option<ModelDefault>,
/// Default thinking level from the settings payload, applied when the CLI
/// does not explicitly override it. `None` → the CLI's level (or the
/// built-in "off" default).
pub thinking_level: Option<ThinkingLevel>,
/// Enabled builtin skill names (pre-#73: `[builtin_skills] enabled`).
pub builtin_skills: Vec<String>,
/// Local dynamic-trigger poll interval, in seconds.
pub trigger_poll_secs: u64,
/// TUI feed scrollback cap (`None` → TUI built-in default).
pub tui_max_feed_lines: Option<u64>,
/// Orchestrator thinking-summary settings (`None` → thinking stays raw).
/// TODO(#73): `WireDaemonConfig` has no thinking-summary fields yet;
/// until the settings proto grows them, [`apply_wire`](Self::apply_wire)
/// cannot populate this and it stays `None` at startup.
pub thinking_summary: Option<ThinkingSummarySettings>,
/// Startup seam for the remaining local reads (issue #73): when `false`,
/// the composition root skips the local-file scans for MCP servers,
/// hooks, LSP servers, templates, skills, and custom models, so a fully
/// controller-provisioned daemon starts without reading ANY local config
/// file. Defaults to `true` to preserve existing behavior — flipping the
/// default becomes possible once the controller provisions those areas
/// through the settings RPC (TODO(#73), tracked with the controller
/// provisioning work). `WireDaemonConfig` has no matching field yet, so
/// only a host embedding the daemon can flip it today.
pub load_local_sources: bool,
/// Controller StorageService endpoint (`host:port`) for controller-backed
/// runtime storage (issue #85). `None` = use `LocalRuntimeStorage`.
pub storage_service_addr: Option<String>,
}
impl Default for StartupConfig {
fn default() -> Self {
Self {
model_default: None,
thinking_level: None,
builtin_skills: Vec::new(),
trigger_poll_secs: DEFAULT_DYNAMIC_TRIGGER_POLL_INTERVAL_SECS,
tui_max_feed_lines: None,
thinking_summary: None,
load_local_sources: true,
storage_service_addr: None,
}
}
}
impl StartupConfig {
/// Defaults merged with an initial settings payload (issue #72 wire
/// shape). An empty payload yields the pure defaults — the
/// config-file-free startup posture.
pub fn from_wire(payload: &WireDaemonConfig) -> Self {
let mut config = Self::default();
config.apply_wire(payload);
config
}
/// Merge a settings patch (initial payload or a later update) into this
/// config, mirroring [`WireDaemonConfig::merge_from`] semantics: a
/// present optional field replaces the current value, a non-empty
/// repeated field replaces the list. Returns the number of setting areas
/// touched (for diagnostics).
pub fn apply_wire(&mut self, patch: &WireDaemonConfig) -> usize {
let mut touched = 0;
// The model default requires the provider/model PAIR — the same rule
// the legacy `[model]` parse enforced (`parse_model_default` rejects
// a half-configured default rather than guessing).
if let (Some(provider), Some(model)) = (patch.provider.as_deref(), patch.model.as_deref()) {
self.model_default = Some(ModelDefault {
provider: provider.to_string(),
model: model.to_string(),
});
touched += 1;
}
if let Some(raw) = patch.thinking_level.as_deref() {
if let Ok(level) = raw.parse::<ThinkingLevel>() {
self.thinking_level = Some(level);
touched += 1;
}
}
if !patch.builtin_skills.is_empty() {
self.builtin_skills = patch.builtin_skills.clone();
touched += 1;
}
if let Some(secs) = patch.trigger_poll_secs {
self.trigger_poll_secs = secs;
touched += 1;
}
if let Some(lines) = patch.tui_max_feed_lines {
self.tui_max_feed_lines = Some(lines);
touched += 1;
}
// TODO(#73): `base_url` / `thinking` patches already take effect at
// runtime (`TurnHost::handle_configure` + `SetModel`), but have no
// startup representation here yet; thinking-summary and
// `load_local_sources` await settings-proto fields.
touched
}
}
#[cfg(test)]
// Keep unit tests colocated per docs/rust-test-files.md conventions when the
// suite grows; for now the module is small enough to test inline.
mod tests {
use super::*;
#[test]
fn defaults_match_the_pre_issue_73_builtin_defaults() {
let config = StartupConfig::default();
assert!(config.model_default.is_none());
assert!(config.builtin_skills.is_empty());
assert_eq!(
config.trigger_poll_secs,
DEFAULT_DYNAMIC_TRIGGER_POLL_INTERVAL_SECS
);
assert_eq!(config.trigger_poll_secs, 600);
assert!(config.tui_max_feed_lines.is_none());
assert!(config.thinking_summary.is_none());
assert!(config.load_local_sources, "local scans stay on by default");
assert!(config.storage_service_addr.is_none());
}
#[test]
fn empty_payload_leaves_defaults_untouched() {
let config = StartupConfig::from_wire(&WireDaemonConfig::default());
assert_eq!(config, StartupConfig::default());
}
#[test]
fn payload_replaces_defaults_field_by_field() {
let payload = WireDaemonConfig {
provider: Some("anthropic".into()),
model: Some("claude-x".into()),
thinking_level: Some("high".into()),
builtin_skills: vec!["debugging".into()],
trigger_poll_secs: Some(30),
tui_max_feed_lines: Some(8000),
..Default::default()
};
let config = StartupConfig::from_wire(&payload);
assert_eq!(
config.model_default.as_ref().map(|d| d.provider.as_str()),
Some("anthropic")
);
assert_eq!(
config.model_default.as_ref().map(|d| d.model.as_str()),
Some("claude-x")
);
assert_eq!(config.thinking_level, Some(ThinkingLevel::High));
assert_eq!(config.builtin_skills, vec!["debugging".to_string()]);
assert_eq!(config.trigger_poll_secs, 30);
assert_eq!(config.tui_max_feed_lines, Some(8000));
}
#[test]
fn invalid_thinking_level_is_ignored() {
let payload = WireDaemonConfig {
thinking_level: Some("turbo".into()),
..Default::default()
};
let mut config = StartupConfig::default();
assert_eq!(config.apply_wire(&payload), 0);
assert!(config.thinking_level.is_none());
}
#[test]
fn half_configured_model_pair_is_ignored() {
// A lone provider (no model id) is not a usable default — same rule
// the legacy `[model]` parse enforced; the env auto-detection keeps
// applying.
let payload = WireDaemonConfig {
provider: Some("anthropic".into()),
..Default::default()
};
let mut config = StartupConfig::default();
assert_eq!(config.apply_wire(&payload), 0);
assert!(config.model_default.is_none());
}
#[test]
fn apply_wire_counts_touched_areas_and_merges_incrementally() {
let mut config = StartupConfig::default();
let first = WireDaemonConfig {
trigger_poll_secs: Some(60),
..Default::default()
};
assert_eq!(config.apply_wire(&first), 1);
assert_eq!(config.trigger_poll_secs, 60);
let second = WireDaemonConfig {
provider: Some("openai".into()),
model: Some("gpt-x".into()),
trigger_poll_secs: Some(15),
..Default::default()
};
assert_eq!(config.apply_wire(&second), 2);
assert_eq!(config.trigger_poll_secs, 15);
assert_eq!(
config.model_default.as_ref().map(|d| d.model.as_str()),
Some("gpt-x")
);
}
}