crabmate 0.5.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! `ConfigBuilder`:嵌入 TOML 分片与用户 `[agent]` / `[tool_registry]` 的合并累加器。
//!
//! 结构按运行域拆分为子结构(与 [`super::types::AgentConfig`] 对齐),见 **`config_builder_sections`**。
//! 由 [`super::assembly`] 与 [`super::env_overrides`] 写入字段,[`super::finalize`] 消费并产出 [`super::types::AgentConfig`]。

mod config_builder_sections;

pub(crate) use config_builder_sections::ConfigBuilder;

use super::source::{AgentRoleRow, AgentSection, ScheduledAgentTaskRow, ToolRegistrySection};

/// 非空 trim 后覆盖 `String` 字段。
pub(super) fn override_string(dst: &mut String, src: Option<String>) {
    if let Some(s) = src {
        let s = s.trim().to_string();
        if !s.is_empty() {
            *dst = s;
        }
    }
}

/// 非空 trim 后覆盖 `Option<String>` 字段。
pub(super) fn override_opt_string_non_empty(dst: &mut Option<String>, src: Option<String>) {
    if let Some(s) = src {
        let s = s.trim().to_string();
        if !s.is_empty() {
            *dst = Some(s);
        }
    }
}

/// trim 后覆盖 `Option<String>`(允许空字符串,如 bearer token 可显式清空)。
pub(super) fn override_opt_string_trimmed(dst: &mut Option<String>, src: Option<&String>) {
    if let Some(s) = src {
        *dst = Some(s.trim().to_string());
    }
}

/// 非空时覆盖 `Option<Vec<String>>`。
pub(super) fn override_opt_vec(dst: &mut Option<Vec<String>>, src: &Option<Vec<String>>) {
    if let Some(ref v) = *src
        && !v.is_empty()
    {
        *dst = Some(v.clone());
    }
}

/// 键存在则覆盖,**允许空列表**(用于关掉嵌入默认 `http_fetch_allowed_prefixes = ["*"]`)。
pub(super) fn override_opt_vec_allow_empty(
    dst: &mut Option<Vec<String>>,
    src: &Option<Vec<String>>,
) {
    if let Some(ref v) = *src {
        *dst = Some(v.clone());
    }
}

impl ConfigBuilder {
    /// 将 `AgentSection` 中有值的字段覆盖到当前累加器。
    pub(super) fn apply_section(&mut self, agent: AgentSection) {
        self.apply_section_identity_prompt_and_lists(&agent);
        self.apply_section_merge_numeric_mid(&agent);
        self.apply_section_merge_numeric_tail_queues(&agent);
        self.apply_section_merge_numeric_tail_sandbox_web_conv(&agent);
        self.apply_section_merge_numeric_tail_context_tool_explain(&agent);
        self.apply_section_merge_numeric_tail_memory_mcp_semantic_intent(&agent);
    }

    /// 标识字段、提示词路径、列表类覆盖。
    fn apply_section_identity_prompt_and_lists(&mut self, agent: &AgentSection) {
        let llm = &mut self.llm;
        override_string(&mut llm.api_base, agent.api_base.clone());
        override_string(&mut llm.model, agent.model.clone());
        override_opt_string_non_empty(&mut llm.planner_model, agent.planner_model.clone());
        override_opt_string_non_empty(&mut llm.executor_model, agent.executor_model.clone());
        override_opt_string_non_empty(
            &mut llm.llm_http_auth_mode_str,
            agent.llm_http_auth_mode.clone(),
        );
        let rp = &mut self.roles_prompts;
        let no_system_prompt_file_in_section = agent.system_prompt_file.is_none();
        let inline_system_prompt_nonempty = agent
            .system_prompt
            .as_ref()
            .is_some_and(|s| !s.trim().is_empty());
        override_opt_string_non_empty(&mut rp.system_prompt_file, agent.system_prompt_file.clone());
        override_string(&mut rp.system_prompt, agent.system_prompt.clone());
        override_opt_string_non_empty(
            &mut rp.default_agent_role_id,
            agent.default_agent_role.clone(),
        );
        rp.coding_workbench_enabled = agent
            .coding_workbench_enabled
            .or(rp.coding_workbench_enabled);
        override_opt_string_non_empty(
            &mut rp.coding_workbench_increment_file,
            agent.coding_workbench_increment_file.clone(),
        );
        override_opt_string_non_empty(
            &mut rp.default_session_mode,
            agent.default_session_mode.clone(),
        );
        if no_system_prompt_file_in_section && inline_system_prompt_nonempty {
            rp.system_prompt_file = None;
        }
        override_opt_string_non_empty(
            &mut self.command_exec.run_command_working_dir,
            agent.run_command_working_dir.clone(),
        );
        override_opt_string_non_empty(
            &mut self.web_search.web_search_provider_str,
            agent.web_search_provider.clone(),
        );
        override_opt_string_non_empty(
            &mut self.per_plan_policy.final_plan_requirement_str,
            agent.final_plan_requirement.clone(),
        );
        override_opt_string_non_empty(
            &mut self.per_plan_policy.planner_executor_mode_str,
            agent.planner_executor_mode.clone(),
        );
        override_opt_string_non_empty(
            &mut self.cursor_rules.cursor_rules_dir,
            agent.cursor_rules_dir.clone(),
        );
        override_opt_string_non_empty(&mut self.skills.skills_dir, agent.skills_dir.clone());
        // 允许空串 / `-`:finalize 时关闭用户/系统层。
        override_opt_string_trimmed(
            &mut self.skills.skills_user_dir,
            agent.skills_user_dir.as_ref(),
        );
        override_opt_string_trimmed(
            &mut self.skills.skills_system_dir,
            agent.skills_system_dir.as_ref(),
        );

        override_opt_string_trimmed(
            &mut self.web_api.web_api_bearer_token,
            agent.web_api_bearer_token.as_ref(),
        );
        if let Some(ref k) = agent.web_search_api_key {
            self.web_search.web_search_api_key = Some(k.clone());
        }

        override_opt_vec(
            &mut self.command_exec.allowed_commands,
            &agent.allowed_commands,
        );
        override_opt_vec_allow_empty(
            &mut self.http_fetch.http_fetch_allowed_prefixes,
            &agent.http_fetch_allowed_prefixes,
        );
        override_opt_vec(
            &mut self.workspace_roots.workspace_allowed_roots,
            &agent.workspace_allowed_roots,
        );
        override_opt_vec(
            &mut self.web_api.web_cors_allowed_origins,
            &agent.web_cors_allowed_origins,
        );
        if let Some(ref v) = agent.web_workspace_pool {
            self.workspace_roots.web_workspace_pool = Some(v.clone());
        }
    }

    /// `Option` 数值与布尔合并(至上下文摘要与健康探测)。
    fn apply_section_merge_numeric_mid(&mut self, agent: &AgentSection) {
        let su = &mut self.session_ui;
        su.max_message_history = agent.max_message_history.or(su.max_message_history);
        let ce = &mut self.command_exec;
        ce.command_timeout_secs = agent.command_timeout_secs.or(ce.command_timeout_secs);
        ce.command_max_output_len = agent.command_max_output_len.or(ce.command_max_output_len);
        ce.allow_external_path_with_approval = agent
            .allow_external_path_with_approval
            .or(ce.allow_external_path_with_approval);
        let samp = &mut self.llm_sampling;
        samp.max_tokens = agent.max_tokens.or(samp.max_tokens);
        samp.llm_context_tokens = agent.llm_context_tokens.or(samp.llm_context_tokens);
        samp.temperature = agent.temperature.or(samp.temperature);
        samp.llm_seed = agent.llm_seed.or(samp.llm_seed);
        let lv = &mut self.llm_vendor;
        lv.llm_reasoning_split = agent.llm_reasoning_split.or(lv.llm_reasoning_split);
        lv.llm_bigmodel_thinking = agent.llm_bigmodel_thinking.or(lv.llm_bigmodel_thinking);
        lv.llm_kimi_thinking_disabled = agent
            .llm_kimi_thinking_disabled
            .or(lv.llm_kimi_thinking_disabled);
        let retry = &mut self.llm_http_retry;
        retry.api_timeout_secs = agent.api_timeout_secs.or(retry.api_timeout_secs);
        retry.api_max_retries = agent.api_max_retries.or(retry.api_max_retries);
        retry.api_retry_delay_secs = agent.api_retry_delay_secs.or(retry.api_retry_delay_secs);
        self.weather_tool.weather_timeout_secs = agent
            .weather_timeout_secs
            .or(self.weather_tool.weather_timeout_secs);
        let ws = &mut self.web_search;
        ws.web_search_timeout_secs = agent.web_search_timeout_secs.or(ws.web_search_timeout_secs);
        ws.web_search_max_results = agent.web_search_max_results.or(ws.web_search_max_results);
        let hf = &mut self.http_fetch;
        hf.http_fetch_timeout_secs = agent.http_fetch_timeout_secs.or(hf.http_fetch_timeout_secs);
        hf.http_fetch_max_response_bytes = agent
            .http_fetch_max_response_bytes
            .or(hf.http_fetch_max_response_bytes);
        override_opt_string_non_empty(&mut hf.http_fetch_user_agent, agent.http_fetch_user_agent.clone());
        let pp = &mut self.per_plan_policy;
        pp.reflection_default_max_rounds = agent
            .reflection_default_max_rounds
            .or(pp.reflection_default_max_rounds);
        pp.plan_rewrite_max_attempts = agent
            .plan_rewrite_max_attempts
            .or(pp.plan_rewrite_max_attempts);
        pp.final_plan_require_strict_workflow_node_coverage = agent
            .final_plan_require_strict_workflow_node_coverage
            .or(pp.final_plan_require_strict_workflow_node_coverage);
        pp.final_plan_semantic_check_enabled = agent
            .final_plan_semantic_check_enabled
            .or(pp.final_plan_semantic_check_enabled);
        pp.final_plan_semantic_check_accept_legacy_text = agent
            .final_plan_semantic_check_accept_legacy_text
            .or(pp.final_plan_semantic_check_accept_legacy_text);
        pp.final_plan_semantic_check_max_non_readonly_tools = agent
            .final_plan_semantic_check_max_non_readonly_tools
            .or(pp.final_plan_semantic_check_max_non_readonly_tools);
        pp.final_plan_semantic_check_max_tokens = agent
            .final_plan_semantic_check_max_tokens
            .or(pp.final_plan_semantic_check_max_tokens);
        let cr = &mut self.cursor_rules;
        cr.cursor_rules_enabled = agent.cursor_rules_enabled.or(cr.cursor_rules_enabled);
        cr.cursor_rules_include_agents_md = agent
            .cursor_rules_include_agents_md
            .or(cr.cursor_rules_include_agents_md);
        cr.cursor_rules_max_chars = agent.cursor_rules_max_chars.or(cr.cursor_rules_max_chars);
        let sk = &mut self.skills;
        sk.skills_enabled = agent.skills_enabled.or(sk.skills_enabled);
        sk.skills_max_chars = agent.skills_max_chars.or(sk.skills_max_chars);
        sk.skills_top_k = agent.skills_top_k.or(sk.skills_top_k);
        let tt = &mut self.tool_transcript;
        tt.tool_message_max_chars = agent.tool_message_max_chars.or(tt.tool_message_max_chars);
        tt.tool_result_envelope_v1 = agent.tool_result_envelope_v1.or(tt.tool_result_envelope_v1);
        tt.sse_tool_call_include_arguments = agent
            .sse_tool_call_include_arguments
            .or(tt.sse_tool_call_include_arguments);
        let ats = &mut self.agent_tool_stats;
        ats.agent_tool_stats_enabled = agent
            .agent_tool_stats_enabled
            .or(ats.agent_tool_stats_enabled);
        ats.agent_tool_stats_window_events = agent
            .agent_tool_stats_window_events
            .or(ats.agent_tool_stats_window_events);
        ats.agent_tool_stats_min_samples = agent
            .agent_tool_stats_min_samples
            .or(ats.agent_tool_stats_min_samples);
        ats.agent_tool_stats_max_chars = agent
            .agent_tool_stats_max_chars
            .or(ats.agent_tool_stats_max_chars);
        ats.agent_tool_stats_warn_below_success_ratio = agent
            .agent_tool_stats_warn_below_success_ratio
            .or(ats.agent_tool_stats_warn_below_success_ratio);
        let te = &mut self.thinking_echo;
        te.thinking_avoid_echo_system_prompt = agent
            .thinking_avoid_echo_system_prompt
            .or(te.thinking_avoid_echo_system_prompt);
        let no_thinking_appendix_file_in_section =
            agent.thinking_avoid_echo_appendix_file.is_none();
        let inline_thinking_appendix_nonempty = agent
            .thinking_avoid_echo_appendix
            .as_ref()
            .is_some_and(|s| !s.trim().is_empty());
        override_opt_string_non_empty(
            &mut te.thinking_avoid_echo_appendix_file,
            agent.thinking_avoid_echo_appendix_file.clone(),
        );
        if let Some(ref s) = agent.thinking_avoid_echo_appendix
            && !s.trim().is_empty()
        {
            te.thinking_avoid_echo_appendix = Some(s.clone());
        }
        if no_thinking_appendix_file_in_section && inline_thinking_appendix_nonempty {
            te.thinking_avoid_echo_appendix_file = None;
        }
        let cp = &mut self.context_pipeline;
        cp.context_char_budget = agent.context_char_budget.or(cp.context_char_budget);
        cp.context_min_messages_after_system = agent
            .context_min_messages_after_system
            .or(cp.context_min_messages_after_system);
        cp.context_token_trigger_percent = agent
            .context_token_trigger_percent
            .or(cp.context_token_trigger_percent);
        cp.context_token_target_percent = agent
            .context_token_target_percent
            .or(cp.context_token_target_percent);
        cp.context_token_safety_margin_tokens = agent
            .context_token_safety_margin_tokens
            .or(cp.context_token_safety_margin_tokens);
        cp.context_summary_trigger_chars = agent
            .context_summary_trigger_chars
            .or(cp.context_summary_trigger_chars);
        cp.context_summary_tail_messages = agent
            .context_summary_tail_messages
            .or(cp.context_summary_tail_messages);
        cp.context_summary_max_tokens = agent
            .context_summary_max_tokens
            .or(cp.context_summary_max_tokens);
        cp.context_summary_transcript_max_chars = agent
            .context_summary_transcript_max_chars
            .or(cp.context_summary_transcript_max_chars);
        if let Some(ref p) = agent.context_summary_system_file {
            cp.context_summary_system_file = Some(p.clone());
        }
        if let Some(ref p) = agent.context_summary_user_file {
            cp.context_summary_user_file = Some(p.clone());
        }
        let wa = &mut self.web_api;
        wa.health_llm_models_probe = agent.health_llm_models_probe.or(wa.health_llm_models_probe);
        wa.health_llm_models_probe_cache_secs = agent
            .health_llm_models_probe_cache_secs
            .or(wa.health_llm_models_probe_cache_secs);
    }

    /// 队列与会话变更列表字段合并。
    fn apply_section_merge_numeric_tail_queues(&mut self, agent: &AgentSection) {
        let cqc = &mut self.chat_queues_cache;
        cqc.chat_queue_max_concurrent = agent
            .chat_queue_max_concurrent
            .or(cqc.chat_queue_max_concurrent);
        cqc.chat_queue_max_pending = agent.chat_queue_max_pending.or(cqc.chat_queue_max_pending);
        cqc.parallel_readonly_tools_max = agent
            .parallel_readonly_tools_max
            .or(cqc.parallel_readonly_tools_max);
        cqc.read_file_turn_cache_max_entries = agent
            .read_file_turn_cache_max_entries
            .or(cqc.read_file_turn_cache_max_entries);
        cqc.readonly_tool_ttl_cache_secs = agent
            .readonly_tool_ttl_cache_secs
            .or(cqc.readonly_tool_ttl_cache_secs);
        cqc.readonly_tool_ttl_cache_max_entries = agent
            .readonly_tool_ttl_cache_max_entries
            .or(cqc.readonly_tool_ttl_cache_max_entries);
        cqc.test_result_cache_enabled = agent
            .test_result_cache_enabled
            .or(cqc.test_result_cache_enabled);
        cqc.test_result_cache_max_entries = agent
            .test_result_cache_max_entries
            .or(cqc.test_result_cache_max_entries);
        let swc = &mut self.session_workspace_changelist;
        swc.session_workspace_changelist_enabled = agent
            .session_workspace_changelist_enabled
            .or(swc.session_workspace_changelist_enabled);
        swc.session_workspace_changelist_max_chars = agent
            .session_workspace_changelist_max_chars
            .or(swc.session_workspace_changelist_max_chars);
    }

    /// 同步工具沙盒、Web API 审计与会话持久化路径合并。
    fn apply_section_merge_numeric_tail_sandbox_web_conv(&mut self, agent: &AgentSection) {
        let sb = &mut self.sync_tool_sandbox;
        override_opt_string_non_empty(
            &mut sb.sync_default_tool_sandbox_mode_str,
            agent.sync_default_tool_sandbox_mode.clone(),
        );
        override_opt_string_non_empty(
            &mut sb.sync_default_tool_sandbox_docker_image,
            agent.sync_default_tool_sandbox_docker_image.clone(),
        );
        override_opt_string_non_empty(
            &mut sb.sync_default_tool_sandbox_docker_network,
            agent.sync_default_tool_sandbox_docker_network.clone(),
        );
        sb.sync_default_tool_sandbox_docker_timeout_secs = agent
            .sync_default_tool_sandbox_docker_timeout_secs
            .or(sb.sync_default_tool_sandbox_docker_timeout_secs);
        override_opt_string_non_empty(
            &mut sb.sync_default_tool_sandbox_docker_user,
            agent.sync_default_tool_sandbox_docker_user.clone(),
        );
        let wa = &mut self.web_api;
        wa.web_api_require_bearer = agent.web_api_require_bearer.or(wa.web_api_require_bearer);
        wa.web_audit_log_write_tools = agent
            .web_audit_log_write_tools
            .or(wa.web_audit_log_write_tools);
        wa.web_audit_trust_x_forwarded_for = agent
            .web_audit_trust_x_forwarded_for
            .or(wa.web_audit_trust_x_forwarded_for);
        wa.allow_insecure_no_auth_for_non_loopback = agent
            .allow_insecure_no_auth_for_non_loopback
            .or(wa.allow_insecure_no_auth_for_non_loopback);
        override_opt_string_non_empty(
            &mut self.conversation_persistence.conversation_store_sqlite_path,
            agent.conversation_store_sqlite_path.clone(),
        );
    }

    /// 上下文引导注入与工具调用解释字段合并。
    fn apply_section_merge_numeric_tail_context_tool_explain(&mut self, agent: &AgentSection) {
        let cbi = &mut self.context_bootstrap_inject;
        cbi.agent_memory_file_enabled = agent
            .agent_memory_file_enabled
            .or(cbi.agent_memory_file_enabled);
        override_opt_string_non_empty(&mut cbi.agent_memory_file, agent.agent_memory_file.clone());
        cbi.agent_memory_file_max_chars = agent
            .agent_memory_file_max_chars
            .or(cbi.agent_memory_file_max_chars);
        cbi.living_docs_inject_enabled = agent
            .living_docs_inject_enabled
            .or(cbi.living_docs_inject_enabled);
        override_opt_string_non_empty(
            &mut cbi.living_docs_relative_dir,
            agent.living_docs_relative_dir.clone(),
        );
        cbi.living_docs_inject_max_chars = agent
            .living_docs_inject_max_chars
            .or(cbi.living_docs_inject_max_chars);
        cbi.living_docs_file_max_each_chars = agent
            .living_docs_file_max_each_chars
            .or(cbi.living_docs_file_max_each_chars);
        cbi.project_profile_inject_enabled = agent
            .project_profile_inject_enabled
            .or(cbi.project_profile_inject_enabled);
        cbi.project_profile_inject_max_chars = agent
            .project_profile_inject_max_chars
            .or(cbi.project_profile_inject_max_chars);
        cbi.project_dependency_brief_inject_enabled = agent
            .project_dependency_brief_inject_enabled
            .or(cbi.project_dependency_brief_inject_enabled);
        cbi.project_dependency_brief_inject_max_chars = agent
            .project_dependency_brief_inject_max_chars
            .or(cbi.project_dependency_brief_inject_max_chars);
        let tce = &mut self.tool_call_explain;
        tce.tool_call_explain_enabled = agent
            .tool_call_explain_enabled
            .or(tce.tool_call_explain_enabled);
        tce.tool_call_explain_min_chars = agent
            .tool_call_explain_min_chars
            .or(tce.tool_call_explain_min_chars);
        tce.tool_call_explain_max_chars = agent
            .tool_call_explain_max_chars
            .or(tce.tool_call_explain_max_chars);
    }

    /// 长期记忆、MCP、语义代码库与意图路由阈值合并。
    fn apply_section_merge_numeric_tail_memory_mcp_semantic_intent(
        &mut self,
        agent: &AgentSection,
    ) {
        let ltm = &mut self.long_term_memory;
        ltm.long_term_memory_enabled = agent
            .long_term_memory_enabled
            .or(ltm.long_term_memory_enabled);
        override_opt_string_non_empty(
            &mut ltm.long_term_memory_scope_mode_str,
            agent.long_term_memory_scope_mode.clone(),
        );
        override_opt_string_non_empty(
            &mut ltm.long_term_memory_vector_backend_str,
            agent.long_term_memory_vector_backend.clone(),
        );
        ltm.long_term_memory_max_entries = agent
            .long_term_memory_max_entries
            .or(ltm.long_term_memory_max_entries);
        ltm.long_term_memory_inject_max_chars = agent
            .long_term_memory_inject_max_chars
            .or(ltm.long_term_memory_inject_max_chars);
        override_opt_string_non_empty(
            &mut ltm.long_term_memory_store_sqlite_path,
            agent.long_term_memory_store_sqlite_path.clone(),
        );
        ltm.long_term_memory_top_k = agent.long_term_memory_top_k.or(ltm.long_term_memory_top_k);
        ltm.long_term_memory_max_chars_per_chunk = agent
            .long_term_memory_max_chars_per_chunk
            .or(ltm.long_term_memory_max_chars_per_chunk);
        ltm.long_term_memory_min_chars_to_index = agent
            .long_term_memory_min_chars_to_index
            .or(ltm.long_term_memory_min_chars_to_index);
        ltm.long_term_memory_async_index = agent
            .long_term_memory_async_index
            .or(ltm.long_term_memory_async_index);
        ltm.long_term_memory_auto_index_turns = agent
            .long_term_memory_auto_index_turns
            .or(ltm.long_term_memory_auto_index_turns);
        ltm.long_term_memory_auto_summarize_experience = agent
            .long_term_memory_auto_summarize_experience
            .or(ltm.long_term_memory_auto_summarize_experience);
        ltm.long_term_memory_prioritize_experience_recall = agent
            .long_term_memory_prioritize_experience_recall
            .or(ltm.long_term_memory_prioritize_experience_recall);
        ltm.long_term_memory_default_ttl_secs = agent
            .long_term_memory_default_ttl_secs
            .or(ltm.long_term_memory_default_ttl_secs);
        let mcp = &mut self.mcp_client;
        mcp.mcp_enabled = agent.mcp_enabled.or(mcp.mcp_enabled);
        override_opt_string_non_empty(&mut mcp.mcp_command, agent.mcp_command.clone());
        mcp.mcp_tool_timeout_secs = agent.mcp_tool_timeout_secs.or(mcp.mcp_tool_timeout_secs);
        let cs = &mut self.codebase_semantic;
        cs.codebase_semantic_search_enabled = agent
            .codebase_semantic_search_enabled
            .or(cs.codebase_semantic_search_enabled);
        cs.codebase_semantic_invalidate_on_workspace_change = agent
            .codebase_semantic_invalidate_on_workspace_change
            .or(cs.codebase_semantic_invalidate_on_workspace_change);
        override_opt_string_non_empty(
            &mut cs.codebase_semantic_index_sqlite_path,
            agent.codebase_semantic_index_sqlite_path.clone(),
        );
        cs.codebase_semantic_max_file_bytes = agent
            .codebase_semantic_max_file_bytes
            .or(cs.codebase_semantic_max_file_bytes);
        cs.codebase_semantic_chunk_max_chars = agent
            .codebase_semantic_chunk_max_chars
            .or(cs.codebase_semantic_chunk_max_chars);
        cs.codebase_semantic_top_k = agent.codebase_semantic_top_k.or(cs.codebase_semantic_top_k);
        cs.codebase_semantic_query_max_chunks = agent
            .codebase_semantic_query_max_chunks
            .or(cs.codebase_semantic_query_max_chunks);
        cs.codebase_semantic_rebuild_max_files = agent
            .codebase_semantic_rebuild_max_files
            .or(cs.codebase_semantic_rebuild_max_files);
        cs.codebase_semantic_rebuild_incremental = agent
            .codebase_semantic_rebuild_incremental
            .or(cs.codebase_semantic_rebuild_incremental);
        cs.codebase_semantic_hybrid_alpha = agent
            .codebase_semantic_hybrid_alpha
            .or(cs.codebase_semantic_hybrid_alpha);
        cs.codebase_semantic_fts_top_n = agent
            .codebase_semantic_fts_top_n
            .or(cs.codebase_semantic_fts_top_n);
        cs.codebase_semantic_hybrid_semantic_pool = agent
            .codebase_semantic_hybrid_semantic_pool
            .or(cs.codebase_semantic_hybrid_semantic_pool);
    }

    pub(super) fn merge_agent_role_rows(&mut self, rows: &[AgentRoleRow]) {
        for row in rows {
            let id = row.id.trim().to_string();
            if id.is_empty() {
                continue;
            }
            super::agent_roles::merge_into_role_entry(
                self.agent_role_entries.entry(id).or_default(),
                row.system_prompt.clone(),
                row.system_prompt_file.clone(),
                row.allowed_tools.clone(),
                row.prepend_coding_workbench,
                row.default_session_mode.clone(),
            );
        }
    }

    pub(super) fn merge_scheduled_agent_task_rows(&mut self, rows: &[ScheduledAgentTaskRow]) {
        for row in rows {
            self.scheduled_agent_task_rows.push(row.clone());
        }
    }

    pub(super) fn apply_tool_registry(&mut self, tr: ToolRegistrySection) {
        apply_tool_registry_timeouts(&mut self.tool_registry_policy, &tr);
        apply_tool_registry_tool_lists(&mut self.tool_registry_policy, tr);
    }
}

fn apply_tool_registry_timeouts(
    p: &mut config_builder_sections::ConfigBuilderToolRegistryPolicy,
    tr: &ToolRegistrySection,
) {
    if let Some(v) = tr.http_fetch_wall_timeout_secs {
        p.tool_registry_http_fetch_wall_timeout_secs = Some(v);
    }
    if let Some(v) = tr.http_request_wall_timeout_secs {
        p.tool_registry_http_request_wall_timeout_secs = Some(v);
    }
    for (k, v) in &tr.parallel_wall_timeout_secs {
        p.tool_registry_parallel_wall_timeout_secs
            .insert(k.clone(), *v);
    }
}

fn apply_tool_registry_tool_lists(
    p: &mut config_builder_sections::ConfigBuilderToolRegistryPolicy,
    tr: ToolRegistrySection,
) {
    if let Some(v) = tr.parallel_sync_denied_tools {
        p.tool_registry_parallel_sync_denied_tools = Some(v);
    }
    if let Some(v) = tr.parallel_sync_denied_prefixes {
        p.tool_registry_parallel_sync_denied_prefixes = Some(v);
    }
    if let Some(v) = tr.sync_default_inline_tools {
        p.tool_registry_sync_default_inline_tools = Some(v);
    }
    if let Some(v) = tr.write_effect_tools {
        p.tool_registry_write_effect_tools = Some(v);
    }
    if let Some(v) = tr.sub_agent_patch_write_extra_tools {
        p.tool_registry_sub_agent_patch_write_extra_tools = Some(v);
    }
    if let Some(v) = tr.sub_agent_test_runner_extra_tools {
        p.tool_registry_sub_agent_test_runner_extra_tools = Some(v);
    }
    if let Some(v) = tr.sub_agent_review_readonly_deny_tools {
        p.tool_registry_sub_agent_review_readonly_deny_tools = Some(v);
    }
    // 后台任务 6 键(字段均 Copy;`.or` 保持既有值,语义与上方 `if let Some` 一致,且不增分支)。
    p.tool_registry_background_jobs_enabled = tr
        .background_jobs_enabled
        .or(p.tool_registry_background_jobs_enabled);
    p.tool_registry_background_job_max_concurrent = tr
        .background_job_max_concurrent
        .or(p.tool_registry_background_job_max_concurrent);
    p.tool_registry_background_job_max_queued = tr
        .background_job_max_queued
        .or(p.tool_registry_background_job_max_queued);
    p.tool_registry_background_job_ttl_secs = tr
        .background_job_ttl_secs
        .or(p.tool_registry_background_job_ttl_secs);
    p.tool_registry_background_job_result_grace_secs = tr
        .background_job_result_grace_secs
        .or(p.tool_registry_background_job_result_grace_secs);
    p.tool_registry_background_job_max_entries = tr
        .background_job_max_entries
        .or(p.tool_registry_background_job_max_entries);
}