- 14164
return Ok(vak_config::FileConfig::default()); - 14165
} - 14166
let raw = std::fs::read_to_string(path).map_err(|error| error.to_string())?; - 14167
toml::from_str(&raw).map_err(|error| error.to_string()) - 14168
} - 14169
- 14170
fn config_layer_response( - 14171
core: &vak_core::Core, - 14172
scope: ConfigScope, - 14173
) -> Result<serde_json::Value, String> { - 14174
let path = scope.config_path(core)?; - 14175
let layer = read_config_layer(&path)?; - 14176
Ok(serde_json::json!({ - 14177
"scope": scope.label(), - 14178
"path": path, - 14179
"provider": layer.provider, - 14180
"model": layer.model, - 14181
"max_tokens": layer.max_tokens, - 14182
"max_turns": layer.max_turns, - 14183
"permission_mode": layer.permission_mode.map(|mode| format!("{mode:?}")), - 14184
"approval_mode": layer.approval_mode.map(|mode| mode.as_str()), - 14185
"profile": layer.profile, - 14186
"workers": layer.workers, - 14187
"theme": layer.ui.theme, - 14188
"permissions": { - 14189
"allow": layer.allow, - 14190
"ask": layer.ask, - 14191
"deny": layer.deny, - 14192
}, - 14193
"memory": { - 14194
"search_enabled": layer.memory.search_enabled, - 14195
"write_enabled": layer.memory.write_enabled, - 14196
"reflection": layer.memory.reflection, - 14197
"skill_proposals": layer.memory.skill_proposals, - 14198
}, - 14199
"work": { - 14200
"enabled": layer.work.enabled, - 14201
"default_mode": layer.work.default_mode, - 14202
"max_items": layer.work.max_items, - 14203
"max_revisions": layer.work.max_revisions, - 14204
"max_parallel": layer.work.max_parallel, - 14205
"confirmation": layer.work.confirmation, - 14206
}, - 14207
"counts": { - 14208
"mcp": layer.mcp.servers.len(), - 14209
"hooks": layer.hooks.len(), - 14210
}, - 14211
"capabilities": { - 14212
"inherit_mcp": layer.capabilities.inherit_mcp, - 14213
"inherit_hooks": layer.capabilities.inherit_hooks, - 14214
"inherit_skills": layer.capabilities.inherit_skills, - 14215
"inherit_commands": layer.capabilities.inherit_commands, - 14216
"inherit_plugins": layer.capabilities.inherit_plugins, - 14217
}, - 14218
})) - 14219
} - 14220
- 14221
async fn get_global_config_layer( - 14222
State(state): State<AppState>, - 14223
axum::extract::Query(q): axum::extract::Query<AgentScopeQuery>, - 14224
) -> axum::response::Response { - 14225
use axum::response::IntoResponse; - 14226
let core = scoped_core!(&state, None, q.agent.as_deref()); - 14227
match config_layer_response(&core, ConfigScope::User) { - 14228
Ok(layer) => Json(layer).into_response(), - 14229
Err(error) => ( - 14230
StatusCode::INTERNAL_SERVER_ERROR, - 14231
Json(serde_json::json!({ "error": error })), - 14232
) - 14233
.into_response(), - 14234
} - 14235
} - 14236
- 14237
async fn get_workspace_config_layer( - 14238
State(state): State<AppState>, - 14239
axum::extract::Query(q): axum::extract::Query<AgentScopeQuery>, - 14240
) -> axum::response::Response { - 14241
use axum::response::IntoResponse; - 14242
let core = scoped_core!(&state, None, q.agent.as_deref()); - 14243
match config_layer_response(&core, ConfigScope::Workspace) { - 14244
Ok(layer) => Json(layer).into_response(), - 14245
Err(error) => ( - 14246
StatusCode::INTERNAL_SERVER_ERROR, - 14247
Json(serde_json::json!({ "error": error })), - 14248
) - 14249
.into_response(), - 14250
} - 14251
} - 14252
- 14253
#[derive(Debug, serde::Deserialize)] - 14254
struct AgentNetworkCapabilityBody { - 14255
workspace: String, - 14256
enabled: bool, - 14257
#[serde(default)] - 14258
allowed_peers: Vec<String>, - 14259
max_message_bytes: Option<usize>, - 14260
} - 14261
- 14262
async fn agent_network_capability( - 14263
State(state): State<AppState>, - 14264
Json(body): Json<AgentNetworkCapabilityBody>, - 14265
) -> impl IntoResponse { - 14266
let workspace = match std::path::Path::new(&body.workspace).canonicalize() { - 14267
Ok(path) if path.is_dir() => path.display().to_string(), - 14268
_ => { - 14269
return ( - 14270
StatusCode::BAD_REQUEST, - 14271
Json(serde_json::json!({"error": "workspace must be an existing directory"})), - 14272
); - 14273
} - 14274
}; - 14275
let peers = match body - 14276
.allowed_peers - 14277
.into_iter() - 14278
.map(|peer| { - 14279
std::path::Path::new(&peer) - 14280
.canonicalize() - 14281
.ok() - 14282
.filter(|path| path.is_dir()) - 14283
.map(|path| path.display().to_string()) - 14284
}) - 14285
.collect::<Option<Vec<_>>>() - 14286
{ - 14287
Some(peers) => peers.into_iter().collect(), - 14288
None => { - 14289
return ( - 14290
StatusCode::BAD_REQUEST, - 14291
Json( - 14292
serde_json::json!({"error": "every allowed peer must be an existing directory"}), - 14293
), - 14294
); - 14295
} - 14296
}; - 14297
let policy = vak_core::agent_network::WorkspaceNetworkPolicy { - 14298
enabled: body.enabled, - 14299
allowed_peers: peers, - 14300
max_message_bytes: body - 14301
.max_message_bytes - 14302
.unwrap_or(256 * 1024) - 14303
.clamp(1, 256 * 1024), - 14304
}; - 14305
let broker = state.core.agent_network_broker(); - 14306
let capability = broker.register(workspace.clone(), policy); - 14307
if let Err(error) = broker.save_policies( - 14308
&state - 14309
.core - 14310
.sessions_home() - 14311
.join("agent-network/policies.json"), - 14312
) { - 14313
let _ = broker.revoke(&capability); - 14314
return ( - 14315
StatusCode::INTERNAL_SERVER_ERROR, - 14316
Json(serde_json::json!({"error": error})), - 14317
); - 14318
} - 14319
( - 14320
StatusCode::OK, - 14321
Json(serde_json::json!({ - 14322
"workspace": workspace, - 14323
"capability": capability.token(), - 14324
"expires_in_seconds": 900 - 14325
})), - 14326
) - 14327
} - 14328
- 14329
#[derive(Debug, serde::Deserialize)] - 14330
struct AgentNetworkSendBody { - 14331
sender_workspace: String, - 14332
capability: String, - 14333
destination_workspace: String, - 14334
body: String, - 14335
} - 14336
- 14337
async fn agent_network_send( - 14338
State(state): State<AppState>, - 14339
Json(body): Json<AgentNetworkSendBody>, - 14340
) -> impl IntoResponse { - 14341
use base64::Engine as _; - 14342
let Ok(payload) = base64::engine::general_purpose::STANDARD.decode(body.body.trim()) else { - 14343
return ( - 14344
StatusCode::BAD_REQUEST, - 14345
Json(serde_json::json!({"error": "body must be standard base64"})), - 14346
); - 14347
}; - 14348
let sender = vak_core::agent_network::BrokerCapability::from_parts( - 14349
body.sender_workspace, - 14350
body.capability, - 14351
); - 14352
match state - 14353
.core - 14354
.agent_network_broker() - 14355
.send_to(&sender, &body.destination_workspace, payload) - 14356
{ - 14357
Ok(()) => ( - 14358
StatusCode::ACCEPTED, - 14359
Json(serde_json::json!({"accepted": true})), - 14360
), - 14361
Err(error) => ( - 14362
StatusCode::FORBIDDEN, - 14363
Json(serde_json::json!({"error": error})), - 14364
), - 14365
} - 14366
} - 14367
- 14368
#[derive(Debug, serde::Deserialize)] - 14369
struct AgentNetworkReceiveQuery { - 14370
workspace: String, - 14371
capability: String, - 14372
} - 14373
- 14374
async fn agent_network_receive( - 14375
State(state): State<AppState>, - 14376
axum::extract::Query(query): axum::extract::Query<AgentNetworkReceiveQuery>, - 14377
) -> impl IntoResponse { - 14378
use base64::Engine as _; - 14379
let receiver = - 14380
vak_core::agent_network::BrokerCapability::from_parts(query.workspace, query.capability); - 14381
match state.core.agent_network_broker().receive(&receiver) { - 14382
Ok(Some(message)) => ( - 14383
StatusCode::OK, - 14384
Json(serde_json::json!({ - 14385
"from_workspace": message.from_workspace, - 14386
"to_workspace": message.to_workspace, - 14387
"body": base64::engine::general_purpose::STANDARD.encode(message.body) - 14388
})), - 14389
), - 14390
Ok(None) => ( - 14391
StatusCode::NO_CONTENT, - 14392
Json(serde_json::json!({"message": null})), - 14393
), - 14394
Err(error) => ( - 14395
StatusCode::FORBIDDEN, - 14396
Json(serde_json::json!({"error": error})), - 14397
), - 14398
} - 14399
} - 14400
- 14401
#[derive(serde::Deserialize, Default)] - 14402
struct ConfigPatch { - 14403
provider: Option<String>, - 14404
model: Option<String>, - 14405
max_turns: Option<usize>, - 14406
permission_mode: Option<String>, - 14407
approval_mode: Option<String>, - 14408
theme: Option<String>, - 14409
#[serde(default)] - 14410
voice_enabled: Option<bool>, - 14411
#[serde(default)] - 14412
voice_max_session_secs: Option<u64>, - 14413
#[serde(default)] - 14414
voice_max_concurrent: Option<usize>, - 14415
#[serde(default)] - 14416
voice_max_audio_bytes: Option<u64>, - 14417
#[serde(default, deserialize_with = "crate::gateway::deserialize_present")] - 14418
voice_provider: Option<Option<String>>, - 14419
#[serde(default, deserialize_with = "crate::gateway::deserialize_present")] - 14420
voice_transcription_model: Option<Option<String>>, - 14421
#[serde(default, deserialize_with = "crate::gateway::deserialize_present")] - 14422
voice_synthesis_model: Option<Option<String>>, - 14423
/// Whether worker delegation (the `task` tool) is available. Absent - 14424
/// means "leave alone", same convention every field here uses. The - 14425
/// `subagents` alias keeps a client sending the pre-rename field name - 14426
/// (a saved script, an unrefreshed admin tab) from silently no-opping. - 14427
#[serde(default, alias = "subagents")] - 14428
workers: Option<bool>, - 14429
/// `[memory]` toggles (docs/design/23-memory.md). Absent means "leave - 14430
/// alone" — same convention every other field here already uses. - 14431
#[serde(default)] - 14432
memory_search_enabled: Option<bool>, - 14433
#[serde(default)] - 14434
memory_write_enabled: Option<bool>, - 14435
#[serde(default)] - 14436
memory_reflection: Option<bool>, - 14437
#[serde(default)] - 14438
memory_skill_proposals: Option<bool>, - 14439
#[serde(default)] - 14440
work_enabled: Option<bool>, - 14441
#[serde(default)] - 14442
work_default_mode: Option<String>, - 14443
#[serde(default)] - 14444
work_max_items: Option<usize>, - 14445
#[serde(default)] - 14446
work_max_revisions: Option<u32>, - 14447
#[serde(default)] - 14448
work_max_parallel: Option<usize>, - 14449
#[serde(default)] - 14450
work_confirmation: Option<String>, - 14451
#[serde(default)] - 14452
inherit_mcp: Option<bool>, - 14453
#[serde(default)] - 14454
inherit_hooks: Option<bool>, - 14455
#[serde(default)] - 14456
inherit_skills: Option<bool>, - 14457
#[serde(default)] - 14458
inherit_commands: Option<bool>, - 14459
#[serde(default)] - 14460
inherit_plugins: Option<bool>, - 14461
/// `[plugins] network_allow` grants for the selected layer (docs/design/ - 14462
/// 39-plugin-ecosystem.md). `Some(names)` grants egress to those plugins; - 14463
/// `Some(vec![])` clears the key (deny-by-default); absent leaves alone. - 14464
/// Grants are privileged and refused for an untrusted project layer. - 14465
#[serde(default)] - 14466
plugins_network_allow: Option<Vec<String>>, - 14467
#[serde(default)] - 14468
agent: Option<String>, - 14469
} - 14470
- 14471
async fn patch_config( - 14472
State(state): State<AppState>, - 14473
Json(body): Json<ConfigPatch>, - 14474
) -> axum::response::Response { - 14475
patch_config_scope(state, body, false).await - 14476
} - 14477
- 14478
async fn patch_global_config( - 14479
State(state): State<AppState>, - 14480
Json(body): Json<ConfigPatch>, - 14481
) -> axum::response::Response { - 14482
patch_config_scope(state, body, true).await - 14483
} - 14484
- 14485
#[derive(serde::Deserialize)] - 14486
struct EvidencePolicyBody { - 14487
seconds: i64, - 14488
#[serde(default)] - 14489
scope: Option<String>, - 14490
} - 14491
- 14492
async fn patch_evidence_policy( - 14493
State(state): State<AppState>, - 14494
Json(body): Json<EvidencePolicyBody>, - 14495
) -> axum::response::Response { - 14496
let path = if body.scope.as_deref() == Some("user") { - 14497
let Some(path) = vak_config::global_path() else { - 14498
return (StatusCode::INTERNAL_SERVER_ERROR, "user home unavailable").into_response(); - 14499
}; - 14500
path - 14501
} else { - 14502
vak_config::project_path(state.core.cwd()) - 14503
}; - 14504
if let Err(error) = vak_config::persist_evidence_max_age(path, body.seconds) { - 14505
return ( - 14506
StatusCode::INTERNAL_SERVER_ERROR, - 14507
Json(serde_json::json!({"error": error.to_string()})), - 14508
) - 14509
.into_response(); - 14510
} - 14511
if state.core.refresh_persisted_preferences().is_err() { - 14512
return ( - 14513
StatusCode::INTERNAL_SERVER_ERROR, - 14514
"could not apply evidence policy", - 14515
) - 14516
.into_response(); - 14517
} - 14518
Json(serde_json::json!({"saved": true, "seconds": body.seconds.max(0)})).into_response() - 14519
} - 14520
- 14521
/// Which keys this write landed on disk but did NOT put into force, because - 14522
/// a narrower layer already pins them. - 14523
/// - 14524
/// The project layer is merged after the global one, so a project - 14525
/// `permission_mode` wins. Writing the global value and then force-applying - 14526
/// it as a runtime override made the live process disagree with what the - 14527
/// files resolve to — right until the next restart, when the project pin - 14528
/// reasserted and the operator's change appeared to have been forgotten. - 14529
/// Persisting and then saying which keys are shadowed is honest; applying - 14530
/// them was not. - 14531
fn shadowed_by_project(core: &vak_core::Core, body: &ConfigPatch) -> Vec<&'static str> { - 14532
let path = vak_config::project_path(core.cwd()); - 14533
// A workspace that IS the default workspace has one file serving as both - 14534
// layers, and `load_with_trust` skips the project pass for exactly that - 14535
// case. Comparing the file against itself made every global write on the - 14536
// gateway's own workspace — the normal case — report as shadowed by a - 14537
// project layer that does not independently exist, telling an operator - 14538
// their change would not take effect when it would. - 14539
if vak_config::global_path().is_some_and(|global| global == path) { - 14540
return Vec::new(); - 14541
} - 14542
let Ok(project) = read_config_layer(&path) else { - 14543
return Vec::new(); - 14544
}; - 14545
let mut out = Vec::new(); - 14546
if body.permission_mode.is_some() && project.permission_mode.is_some() { - 14547
out.push("permission_mode"); - 14548
} - 14549
if body.approval_mode.is_some() && project.approval_mode.is_some() { - 14550
out.push("approval_mode"); - 14551
} - 14552
if body.plugins_network_allow.is_some() && project.plugins.network_allow.is_some() { - 14553
out.push("plugins_network_allow"); - 14554
} - 14555
out - 14556
} - 14557
- 14558
async fn patch_config_scope( - 14559
state: AppState, - 14560
body: ConfigPatch, - 14561
global: bool, - 14562
) -> axum::response::Response { - 14563
let core = scoped_core!(&state, None, body.agent.as_deref()); - 14564
if global - 14565
&& (body.inherit_mcp.is_some() - 14566
|| body.inherit_hooks.is_some() - 14567
|| body.inherit_skills.is_some() - 14568
|| body.inherit_commands.is_some() - 14569
|| body.inherit_plugins.is_some()) - 14570
{ - 14571
return StatusCode::BAD_REQUEST.into_response(); - 14572
} - 14573
if body - 14574
.provider - 14575
.as_deref() - 14576
.is_some_and(|value| value.trim().is_empty()) - 14577
|| body - 14578
.model - 14579
.as_deref() - 14580
.is_some_and(|value| value.trim().is_empty()) - 14581
|| body - 14582
.max_turns - 14583
.is_some_and(|value| !(1..=1000).contains(&value)) - 14584
|| body - 14585
.theme - 14586
.as_deref() - 14587
.is_some_and(|value| !matches!(value, "dark" | "light" | "plain")) - 14588
|| body - 14589
.permission_mode - 14590
.as_deref() - 14591
.is_some_and(|value| parse_mode(value).is_none()) - 14592
|| body - 14593
.approval_mode - 14594
.as_deref() - 14595
.is_some_and(|value| parse_approval_mode(value).is_none()) - 14596
|| body - 14597
.work_default_mode - 14598
.as_deref() - 14599
.is_some_and(|value| !matches!(value, "direct" | "managed" | "auto")) - 14600
|| body - 14601
.work_confirmation - 14602
.as_deref() - 14603
.is_some_and(|value| !matches!(value, "risk-based" | "always" | "never")) - 14604
|| body - 14605
.work_max_items - 14606
.is_some_and(|value| !(1..=128).contains(&value)) - 14607
|| body - 14608
.work_max_revisions - 14609
.is_some_and(|value| !(1..=64).contains(&value)) - 14610
|| body - 14611
.work_max_parallel - 14612
.is_some_and(|value| !(1..=32).contains(&value)) - 14613
{ - 14614
return StatusCode::BAD_REQUEST.into_response(); - 14615
} - 14616
let permission_mode = body.permission_mode.as_deref().and_then(parse_mode); - 14617
let approval_mode = body.approval_mode.as_deref().and_then(parse_approval_mode); - 14618
let voice_patch = vak_config::VoicePatch { - 14619
enabled: body.voice_enabled, - 14620
max_session_secs: body.voice_max_session_secs, - 14621
max_concurrent: body.voice_max_concurrent, - 14622
max_audio_bytes: body.voice_max_audio_bytes, - 14623
provider: body.voice_provider.clone(), - 14624
transcription_model: body.voice_transcription_model.clone(), - 14625
synthesis_model: body.voice_synthesis_model.clone(), - 14626
}; - 14627
let invalid_name = |value: &String| value.trim().is_empty() || value.chars().count() > 256; - 14628
if voice_patch - 14629
.max_session_secs - 14630
.is_some_and(|v| !(1..=86_400).contains(&v)) - 14631
|| voice_patch - 14632
.max_concurrent - 14633
.is_some_and(|v| !(1..=64).contains(&v)) - 14634
|| voice_patch - 14635
.max_audio_bytes - 14636
.is_some_and(|v| !(1..=256 * 1024 * 1024).contains(&v)) - 14637
|| voice_patch - 14638
.provider - 14639
.as_ref() - 14640
.and_then(Option::as_ref) - 14641
.is_some_and(|v| v.parse::<vak_voice::VoiceProvider>().is_err()) - 14642
|| [ - 14643
voice_patch.transcription_model.as_ref(), - 14644
voice_patch.synthesis_model.as_ref(), - 14645
] - 14646
.into_iter() - 14647
.flatten() - 14648
.flatten() - 14649
.any(invalid_name) - 14650
{ - 14651
return StatusCode::BAD_REQUEST.into_response(); - 14652
} - 14653
if !voice_patch.is_empty() { - 14654
let path = if global { - 14655
vak_config::global_path().ok_or_else(|| vak_config::ConfigError::Write { - 14656
path: std::path::PathBuf::from("<user-config>"), - 14657
source: std::io::Error::other("user home unavailable"), - 14658
}) - 14659
} else { - 14660
Ok(vak_config::project_path(core.cwd())) - 14661
}; - 14662
let result = - 14663
path.and_then(|path| vak_config::persist_voice_settings_at(path, &voice_patch)); - 14664
if result.is_err() { - 14665
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14666
} - 14667
// Re-read the persisted layered value and publish it immediately; - 14668
// new voice sessions observe the change without a restart. - 14669
if let Ok(effective) = - 14670
vak_config::load_with_trust(core.cwd(), core.project_config_trusted()) - 14671
{ - 14672
core.apply_persisted_voice(effective.voice); - 14673
} - 14674
} - 14675
let current_route = core.effective_route(); - 14676
let route_change = body.provider.is_some() || body.model.is_some(); - 14677
let provider = route_change.then(|| { - 14678
body.provider - 14679
.as_deref() - 14680
.map(str::trim) - 14681
.unwrap_or(¤t_route.provider) - 14682
.to_string() - 14683
}); - 14684
let model = route_change.then(|| { - 14685
body.model - 14686
.as_deref() - 14687
.map(str::trim) - 14688
.unwrap_or(¤t_route.model) - 14689
.to_string() - 14690
}); - 14691
if (body.provider.is_some() - 14692
|| body.model.is_some() - 14693
|| body.max_turns.is_some() - 14694
|| permission_mode.is_some() - 14695
|| approval_mode.is_some() - 14696
|| body.theme.is_some()) - 14697
&& (if global { - 14698
vak_config::persist_global_preferences( - 14699
provider.as_deref(), - 14700
model.as_deref(), - 14701
body.max_turns, - 14702
permission_mode, - 14703
approval_mode, - 14704
body.theme.as_deref(), - 14705
) - 14706
} else { - 14707
vak_config::persist_project_preferences( - 14708
core.cwd(), - 14709
provider.as_deref(), - 14710
model.as_deref(), - 14711
body.max_turns, - 14712
permission_mode, - 14713
approval_mode, - 14714
body.theme.as_deref(), - 14715
) - 14716
}) - 14717
.is_err() - 14718
{ - 14719
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14720
} - 14721
// Only a global write can be shadowed: the project layer is the last - 14722
// one merged, so a project write is already the winner. - 14723
let shadowed: Vec<&'static str> = if global { - 14724
shadowed_by_project(&core, &body) - 14725
} else { - 14726
Vec::new() - 14727
}; - 14728
let mut changes = Vec::new(); - 14729
if let (Some(provider), Some(model)) = (provider, model) { - 14730
let effective = vak_config::load_with_trust(core.cwd(), core.project_config_trusted()); - 14731
let Ok(effective) = effective else { - 14732
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14733
}; - 14734
core.apply_persisted_route(effective.provider, effective.model); - 14735
changes.push(format!("route={provider}/{model}")); - 14736
} - 14737
if let Some(max_turns) = body.max_turns { - 14738
if !(1..=1000).contains(&max_turns) { - 14739
return StatusCode::BAD_REQUEST.into_response(); - 14740
} - 14741
core.apply_persisted_max_turns(max_turns); - 14742
changes.push(format!("max_turns={max_turns}")); - 14743
} - 14744
if let Some(mode) = &body.permission_mode { - 14745
let Some(mode) = parse_mode(mode) else { - 14746
return StatusCode::BAD_REQUEST.into_response(); - 14747
}; - 14748
// Persisted above either way; only put into force when nothing - 14749
// narrower already pins it, so the live value and the files agree. - 14750
if shadowed.contains(&"permission_mode") { - 14751
changes.push(format!("permission_mode={mode:?} (persisted, shadowed)")); - 14752
} else { - 14753
apply_permission_mode(&core, &state, mode, true); - 14754
changes.push(format!("permission_mode={mode:?}")); - 14755
} - 14756
} - 14757
if let Some(raw) = &body.approval_mode { - 14758
let Some(mode) = parse_approval_mode(raw) else { - 14759
return StatusCode::BAD_REQUEST.into_response(); - 14760
}; - 14761
if shadowed.contains(&"approval_mode") { - 14762
changes.push(format!( - 14763
"approval_mode={} (persisted, shadowed)", - 14764
mode.as_str() - 14765
)); - 14766
} else { - 14767
core.apply_persisted_approval_mode(mode); - 14768
changes.push(format!("approval_mode={}", mode.as_str())); - 14769
} - 14770
} - 14771
if let Some(theme) = body.theme { - 14772
if !matches!(theme.as_str(), "dark" | "light" | "plain") { - 14773
return StatusCode::BAD_REQUEST.into_response(); - 14774
} - 14775
changes.push(format!("theme={theme}")); - 14776
core.apply_persisted_theme(theme); - 14777
} - 14778
if let Some(workers) = body.workers { - 14779
let persisted = if global { - 14780
vak_config::persist_global_workers(workers) - 14781
} else { - 14782
vak_config::persist_project_workers(core.cwd(), workers) - 14783
}; - 14784
if persisted.is_err() { - 14785
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14786
} - 14787
core.apply_persisted_workers(workers); - 14788
changes.push(format!("workers={workers}")); - 14789
} - 14790
if body.memory_search_enabled.is_some() - 14791
|| body.memory_write_enabled.is_some() - 14792
|| body.memory_reflection.is_some() - 14793
|| body.memory_skill_proposals.is_some() - 14794
{ - 14795
let persisted = if global { - 14796
vak_config::persist_global_memory_prefs( - 14797
body.memory_search_enabled, - 14798
body.memory_write_enabled, - 14799
body.memory_reflection, - 14800
body.memory_skill_proposals, - 14801
) - 14802
} else { - 14803
vak_config::persist_project_memory_prefs( - 14804
core.cwd(), - 14805
body.memory_search_enabled, - 14806
body.memory_write_enabled, - 14807
body.memory_reflection, - 14808
body.memory_skill_proposals, - 14809
) - 14810
}; - 14811
if persisted.is_err() { - 14812
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14813
} - 14814
// `apply_persisted_memory` sets all four flags at once, so fields - 14815
// this PATCH didn't mention keep their current effective value - 14816
// rather than reverting to whatever was on disk before. - 14817
core.apply_persisted_memory( - 14818
body.memory_search_enabled - 14819
.unwrap_or_else(|| core.effective_memory_search_enabled()), - 14820
body.memory_write_enabled - 14821
.unwrap_or_else(|| core.effective_memory_write_enabled()), - 14822
body.memory_reflection - 14823
.unwrap_or_else(|| core.effective_memory_reflection()), - 14824
body.memory_skill_proposals - 14825
.unwrap_or_else(|| core.effective_memory_skill_proposals()), - 14826
); - 14827
changes.push(format!( - 14828
"memory(search={}, write={}, reflection={}, skill_proposals={})", - 14829
core.effective_memory_search_enabled(), - 14830
core.effective_memory_write_enabled(), - 14831
core.effective_memory_reflection(), - 14832
core.effective_memory_skill_proposals(), - 14833
)); - 14834
} - 14835
if body.work_enabled.is_some() - 14836
|| body.work_default_mode.is_some() - 14837
|| body.work_max_items.is_some() - 14838
|| body.work_max_revisions.is_some() - 14839
|| body.work_max_parallel.is_some() - 14840
|| body.work_confirmation.is_some() - 14841
{ - 14842
let path = if global { - 14843
vak_config::global_path().ok_or(StatusCode::INTERNAL_SERVER_ERROR) - 14844
} else { - 14845
Ok(vak_config::project_path(core.cwd())) - 14846
}; - 14847
let Ok(path) = path else { - 14848
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14849
}; - 14850
if vak_config::persist_work_preferences( - 14851
path, - 14852
body.work_enabled, - 14853
body.work_default_mode.as_deref(), - 14854
body.work_max_items, - 14855
body.work_max_revisions, - 14856
body.work_max_parallel, - 14857
body.work_confirmation.as_deref(), - 14858
) - 14859
.is_err() - 14860
{ - 14861
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14862
} - 14863
let resolved = vak_config::load_with_trust(core.cwd(), core.project_config_trusted()); - 14864
let Ok(resolved) = resolved else { - 14865
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14866
}; - 14867
core.apply_persisted_work(resolved.work); - 14868
changes.push(format!( - 14869
"work(mode={}, enabled={})", - 14870
core.effective_work().default_mode, - 14871
core.effective_work().enabled - 14872
)); - 14873
} - 14874
if let Some(grant) = &body.plugins_network_allow { - 14875
if !grant.is_empty() && !global && !core.project_config_trusted() { - 14876
return ( - 14877
StatusCode::FORBIDDEN, - 14878
Json(serde_json::json!({ - 14879
"error": "plugins.network_allow is a privileged key: an untrusted \ - 14880
workspace may not grant its execution plugins network \ - 14881
egress. Set it from the Trusted (global) settings instead." - 14882
})), - 14883
) - 14884
.into_response(); - 14885
} - 14886
let path = if global { - 14887
vak_config::global_path().ok_or(StatusCode::INTERNAL_SERVER_ERROR) - 14888
} else { - 14889
Ok(vak_config::project_path(core.cwd())) - 14890
}; - 14891
let Ok(path) = path else { - 14892
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14893
}; - 14894
if vak_config::persist_plugins_network_allow(&path, Some(grant.clone())).is_err() { - 14895
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14896
} - 14897
if core.refresh_persisted_preferences().is_err() { - 14898
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14899
} - 14900
if shadowed.contains(&"plugins_network_allow") { - 14901
changes.push(format!( - 14902
"plugins_network_allow={grant:?} (persisted, shadowed by project)" - 14903
)); - 14904
} else { - 14905
changes.push(format!("plugins_network_allow={grant:?}")); - 14906
} - 14907
} - 14908
if body.inherit_mcp.is_some() - 14909
|| body.inherit_hooks.is_some() - 14910
|| body.inherit_skills.is_some() - 14911
|| body.inherit_commands.is_some() - 14912
|| body.inherit_plugins.is_some() - 14913
{ - 14914
let path = vak_config::project_path(core.cwd()); - 14915
if vak_config::persist_capability_inheritance( - 14916
&path, - 14917
body.inherit_mcp, - 14918
body.inherit_hooks, - 14919
body.inherit_skills, - 14920
body.inherit_commands, - 14921
body.inherit_plugins, - 14922
) - 14923
.is_err() - 14924
{ - 14925
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14926
} - 14927
let Ok(resolved) = vak_config::load_with_trust(core.cwd(), core.project_config_trusted()) - 14928
else { - 14929
return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - 14930
}; - 14931
core.apply_persisted_capability_inheritance(resolved.capabilities); - 14932
core.apply_persisted_mcp_servers(resolved.mcp); - 14933
core.apply_persisted_hooks(resolved.hooks); - 14934
changes.push("capability_inheritance".into()); - 14935
} - 14936
if !changes.is_empty() { - 14937
vak_core::security_events::record( - 14938
&core.sessions_home(), - 14939
vak_core::security_events::EventKind::ConfigChange, - 14940
"config_patched", - 14941
&changes.join(", "), - 14942
None, - 14943
); - 14944
state - 14945
.hub - 14946
.emit_config_changed("config_patched", &changes.join(", ")); - 14947
} - 14948
( - 14949
StatusCode::OK, - 14950
Json(serde_json::json!({ - 14951
"applied": changes, - 14952
// Named, so a client can tell the operator "saved, but this - 14953
// project overrides it" instead of reporting a clean success - 14954
// that the next restart quietly undoes. - 14955
"shadowed_by_project": shadowed, - 14956
})), - 14957
) - 14958
.into_response() - 14959
} - 14960
- 14961
// ---- MCP server management -------------------------------------------------- - 14962
// - 14963
// The desktop Settings page edits the MCP table here: GET reads the - 14964
// effective table; PUT validates, persists to the project config.toml - 14965
// ([mcp.servers]) and hot-applies into the running Core so the next turn - 14966
// picks it up without a backend restart. mcp.servers is a privileged key: - 14967
// this endpoint is only reachable through the bearer-token router of a - 14968
// locally trusted surface. - 14969
- 14970
/// Project-only, deliberately not `core.effective_mcp()` (the merged - 14971
/// effective set, global layer included). `PUT /config/mcp` writes whatever - 14972
/// this reports straight into the *project* config — reporting the merged - 14973
/// set would silently fork every currently-inherited global MCP server - 14974
/// into the project file the moment any one server was added, edited, or - 14975
/// removed here, freezing that project's copy out of future changes to the - 14976
/// global definition. Unlike the hooks list (a plain `Vec` extended with no - 14977
/// dedup key), the merge for MCP servers is a name-keyed map — so this - 14978
/// couldn't duplicate or compound the way the hooks bug did, but it would - 14979
/// still quietly diverge project config from what the operator thought - 14980
/// they were changing. Mirrors `get_global_mcp_servers`, which has always - 14981
/// read its own file directly for the same reason. - 14982
async fn get_mcp_servers( - 14983
State(state): State<AppState>, - 14984
axum::extract::Query(q): axum::extract::Query<AgentScopeQuery>, - 14985
) -> axum::response::Response { - 14986
use axum::response::IntoResponse; - 14987
let core = scoped_core!(&state, None, q.agent.as_deref()); - 14988
let path = vak_config::project_path(core.cwd()); - 14989
match read_mcp_config(&path) { - 14990
Ok(mcp) => Json(serde_json::json!({ "servers": mcp.servers })).into_response(), - 14991
Err(error) => ( - 14992
StatusCode::INTERNAL_SERVER_ERROR, - 14993
Json(serde_json::json!({ "error": error })), - 14994
) - 14995
.into_response(), - 14996
} - 14997
} - 14998
- 14999
#[derive(serde::Deserialize, Clone)] - 15000
struct HookInput { - 15001
event: String, - 15002
#[serde(default)] - 15003
matcher: Option<String>, - 15004
command: String, - 15005
#[serde(default)] - 15006
timeout_ms: Option<u64>, - 15007
#[serde(default = "default_hook_enabled")] - 15008
enabled: bool, - 15009
#[serde(default)] - 15010
failure_mode: Option<String>, - 15011
} - 15012
- 15013
fn default_hook_enabled() -> bool { - 15014
true - 15015
} - 15016
- 15017
#[derive(serde::Deserialize)] - 15018
struct HooksPutBody { - 15019
hooks: Vec<HookInput>, - 15020
#[serde(default)] - 15021
agent: Option<String>, - 15022
} - 15023
- 15024
/// Project-only, deliberately not `state.core.config().hooks` (the merged - 15025
/// effective list, global layer included — `vak_config::merge_into` extends - 15026
/// the project's hooks with the global ones on every load). `PUT - 15027
/// /config/hooks` replaces the *project* file's own `[[hooks]]` array with - 15028
/// whatever this endpoint reported; reporting the merged list would hand - 15029
/// back an inherited global hook, which the next save would then write into - 15030
/// the project file as if it were the project's own — duplicating it there, - 15031
/// and compounding on every subsequent edit as the merge re-extends over an - 15032
/// already-doubled list. Mirrors `get_global_hooks`, which has always read - 15033
/// its own file directly for the same reason. - 15034
/// Prompt layers (docs/design/45-prompt-layers.md). - 15035
/// - 15036
/// `GET /config/prompts?scope=` reports **only** the selected layer's own - 15037
/// text, never the merged view — AGENTS.md rule 21: this GET seeds a - 15038
/// same-shape PUT, and returning inherited text would write a parent layer's - 15039
/// content into the child file on the next save. The effective composition - 15040
/// is a separate endpoint on purpose. - 15041
async fn get_prompt_layer( - 15042
State(state): State<AppState>, - 15043
Query(query): Query<ScopeQuery>, - 15044
) -> axum::response::Response { - 15045
use axum::response::IntoResponse; - 15046
let core = scoped_core!(&state, None, query.agent.as_deref()); - 15047
let dir = vak_core::prompts::layer_dir(&query.scope.prompt_root(&core)); - 15048
let content = vak_core::prompts::read_layer(&dir); - 15049
Json(serde_json::json!({ - 15050
"scope": query.scope.label(), - 15051
"path": dir.display().to_string(), - 15052
"layer": content, - 15053
})) - 15054
.into_response() - 15055
} - 15056
- 15057
#[derive(serde::Deserialize)] - 15058
struct PromptBlockBody { - 15059
scope: ConfigScope, - 15060
block: String, - 15061
/// Absent or null resets the block and resumes inheritance. - 15062
#[serde(default)] - 15063
text: Option<String>, - 15064
#[serde(default)] - 15065
agent: Option<String>, - 15066
} - 15067
- 15068
async fn put_prompt_block( - 15069
State(state): State<AppState>, - 15070
Json(body): Json<PromptBlockBody>, - 15071
) -> axum::response::Response { - 15072
use axum::response::IntoResponse; - 15073
let core = scoped_core!(&state, None, body.agent.as_deref()); - 15074
let Some(block) = vak_core::prompts::PromptBlock::parse(&body.block) else { - 15075
return ( - 15076
StatusCode::BAD_REQUEST, - 15077
Json(serde_json::json!({ - 15078
"error": format!("unknown block '{}'", body.block), - 15079
"editable": ["identity", "operating-rules", "guardrails"], - 15080
"note": "the capability contract, Surface line, and skill/MCP lists are code-owned", - 15081
})), - 15082
) - 15083
.into_response(); - 15084
}; - 15085
// A prompt is spent on every turn of every session, so an unbounded - 15086
// editor is a permanent context tax rather than a one-off mistake. - 15087
const MAX_BLOCK_BYTES: usize = 24_000; - 15088
if let Some(text) = body.text.as_deref() - 15089
&& text.len() > MAX_BLOCK_BYTES - 15090
{ - 15091
return ( - 15092
StatusCode::PAYLOAD_TOO_LARGE, - 15093
Json(serde_json::json!({ - 15094
"error": format!("{} is {}B; the cap is {MAX_BLOCK_BYTES}B", block.slug(), text.len()), - 15095
})), - 15096
) - 15097
.into_response(); - 15098
} - 15099
let dir = vak_core::prompts::layer_dir(&body.scope.prompt_root(&core)); - 15100
match vak_core::prompts::write_block(&dir, block, body.text.as_deref()) { - 15101
Ok(()) => Json(serde_json::json!({ - 15102
"ok": true, - 15103
"scope": body.scope.label(), - 15104
"block": block.slug(), - 15105
"reset": body.text.is_none(), - 15106
// Prompts freeze into the session contract, so the UI must not - 15107
// imply a running turn changed under the user. - 15108
"applies": "new sessions", - 15109
})) - 15110
.into_response(), - 15111
Err(error) => ( - 15112
StatusCode::INTERNAL_SERVER_ERROR, - 15113
Json(serde_json::json!({ "error": error.to_string() })), - 15114
) - 15115
.into_response(), - 15116
} - 15117
} - 15118
- 15119
/// The assembled prompt plus per-layer provenance — the right-hand pane. - 15120
async fn get_prompt_effective( - 15121
State(state): State<AppState>, - 15122
axum::extract::Query(q): axum::extract::Query<AgentScopeQuery>, - 15123
) -> axum::response::Response { - 15124
use axum::response::IntoResponse; - 15125
let core = scoped_core!(&state, None, q.agent.as_deref()); - 15126
Json(prompt_effective_payload(&core)).into_response() - 15127
} - 15128
- 15129
async fn get_agents( - 15130
State(state): State<AppState>, - 15131
axum::extract::Query(query): axum::extract::Query<HashMap<String, String>>, - 15132
) -> axum::response::Response { - 15133
use axum::response::IntoResponse; - 15134
let root = match query - 15135
.get("scope") - 15136
.map(String::as_str) - 15137
.unwrap_or("workspace") - 15138
{ - 15139
"user" => vak_config::paths::default_workspace(), - 15140
"workspace" => state.active_core().cwd().clone(), - 15141
_ => { - 15142
return ( - 15143
StatusCode::BAD_REQUEST, - 15144
Json(serde_json::json!({"error": "invalid agent scope"})), - 15145
) - 15146
.into_response(); - 15147
} - 15148
}; - 15149
match agents::load(&root) { - 15150
Ok(agents) => Json(serde_json::json!({ "agents": agents })).into_response(), - 15151
Err(error) => ( - 15152
StatusCode::INTERNAL_SERVER_ERROR, - 15153
Json(serde_json::json!({ "error": error })), - 15154
) - 15155
.into_response(), - 15156
} - 15157
} - 15158
- 15159
async fn list_agent_templates() -> axum::response::Response { - 15160
use axum::response::IntoResponse; - 15161
Json(serde_json::json!({ - 15162
"templates": agents::builtin_templates() - 15163
}))
Indexing the workspace…
Vakyartha documentation is discovering safe artifacts, anchors, and source references.