strands-harness 0.1.0

A preconfigured Strands agent in one call
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Portable harness configuration.
//!
//! [`HarnessAgentOptions`](crate::HarnessAgentOptions) is the runtime API and takes live SDK
//! objects; [`HarnessAgentConfig`] is the JSON form of the same agent definition, shared with the
//! other Strands harnesses. Live values are module references there, which Rust cannot load yet:
//! [`harness_agent_options_from_config`] rejects them with a clear error.

mod options;
mod validate;

use std::fmt;
use std::str::FromStr;
use std::sync::LazyLock;

use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
use strands::types::JsonObject;

use crate::defaults::{DEFAULT_BUILTIN_PLUGINS, DEFAULT_BUILTIN_TOOLS, DEFAULT_EFFORT, DEFAULT_MODEL};
use crate::types::{
    Effort, ProgrammaticToolCallerConfig, ReadConfig, SessionConfig, Setting, ShellConfig, SubagentConfig,
    WebFetchTransport, WebSearchSetting, json_string,
};

pub use options::harness_agent_options_from_config;
#[cfg(test)]
pub(crate) use options::harness_agent_options_from_config_with_env;
pub(crate) use validate::normalize_harness_agent_config;

/// A built-in tool (`BuiltinToolName`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BuiltinToolName {
    /// `shell`.
    Shell,
    /// `read`.
    Read,
    /// `write`.
    Write,
    /// `edit`.
    Edit,
    /// `web_fetch`.
    WebFetch,
    /// `web_search`.
    WebSearch,
    /// `programmatic_tool_caller`.
    ProgrammaticToolCaller,
    /// `subagent`.
    Subagent,
}

/// Every built-in tool, in canonical order (`BUILTIN_TOOL_NAMES`).
pub const BUILTIN_TOOL_NAMES: &[BuiltinToolName] = &[
    BuiltinToolName::Shell,
    BuiltinToolName::Read,
    BuiltinToolName::Write,
    BuiltinToolName::Edit,
    BuiltinToolName::WebFetch,
    BuiltinToolName::WebSearch,
    BuiltinToolName::ProgrammaticToolCaller,
    BuiltinToolName::Subagent,
];

impl BuiltinToolName {
    /// The tool name, such as `"web_fetch"`.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Shell => "shell",
            Self::Read => "read",
            Self::Write => "write",
            Self::Edit => "edit",
            Self::WebFetch => "web_fetch",
            Self::WebSearch => "web_search",
            Self::ProgrammaticToolCaller => "programmatic_tool_caller",
            Self::Subagent => "subagent",
        }
    }

    /// The keys its per-tool config takes (`BUILTIN_TOOL_CONFIG_KEYS`), or `None` for a tool
    /// that takes only `true` or `false`.
    #[must_use]
    pub fn config_keys(self) -> Option<&'static [&'static str]> {
        BUILTIN_TOOL_CONFIG_KEYS
            .iter()
            .find(|(name, _)| *name == self)
            .map(|(_, keys)| *keys)
    }

    pub(crate) fn names() -> String {
        BUILTIN_TOOL_NAMES
            .iter()
            .map(|name| name.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    }
}

impl fmt::Display for BuiltinToolName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for BuiltinToolName {
    type Err = strands::Error;

    /// Parses a tool name; an unknown one fails listing the available tools.
    fn from_str(value: &str) -> strands::Result<Self> {
        BUILTIN_TOOL_NAMES
            .iter()
            .copied()
            .find(|name| name.as_str() == value)
            .ok_or_else(|| {
                strands::Error::other(format!(
                    "Unknown built-in tool {}. Available: {}.",
                    json_string(value),
                    Self::names()
                ))
            })
    }
}

/// The config keys each configurable built-in accepts (`BUILTIN_TOOL_CONFIG_KEYS`).
pub(crate) const BUILTIN_TOOL_CONFIG_KEYS: &[(BuiltinToolName, &[&str])] = &[
    (BuiltinToolName::Read, &["media"]),
    (BuiltinToolName::Shell, &["description"]),
    (BuiltinToolName::WebFetch, &["model", "transport"]),
    (BuiltinToolName::ProgrammaticToolCaller, &["allowedTools", "timeout"]),
    (BuiltinToolName::Subagent, &["maxDepth"]),
];

/// A built-in feature plugin (`BuiltinPluginName`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BuiltinPluginName {
    /// `environment`: platform, date, working directory and project docs before each user turn.
    Environment,
}

/// Every built-in plugin (`BUILTIN_PLUGIN_NAMES`).
pub const BUILTIN_PLUGIN_NAMES: &[BuiltinPluginName] = &[BuiltinPluginName::Environment];

impl BuiltinPluginName {
    /// The plugin's option name, such as `"environment"`.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Environment => "environment",
        }
    }
}

impl fmt::Display for BuiltinPluginName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for BuiltinPluginName {
    type Err = strands::Error;

    /// Parses a plugin name; an unknown one fails listing the available plugins.
    fn from_str(value: &str) -> strands::Result<Self> {
        BUILTIN_PLUGIN_NAMES
            .iter()
            .copied()
            .find(|name| name.as_str() == value)
            .ok_or_else(|| {
                let mut available: Vec<&str> = BUILTIN_PLUGIN_NAMES.iter().map(|name| name.as_str()).collect();
                available.sort_unstable();
                strands::Error::other(format!(
                    "Unknown built-in plugin {}. Available: {}.",
                    json_string(value),
                    available.join(", ")
                ))
            })
    }
}

/// What a module reference provides (`HarnessModuleKind`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum HarnessModuleKind {
    /// `tool`.
    Tool,
    /// `subagent`.
    Subagent,
    /// `plugin`.
    Plugin,
    /// `sandbox`.
    Sandbox,
    /// `memory-store`.
    MemoryStore,
    /// `model`.
    Model,
    /// `intervention`.
    Intervention,
    /// `agent-config`.
    AgentConfig,
}

impl HarnessModuleKind {
    /// The config spelling, such as `"memory-store"`.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Tool => "tool",
            Self::Subagent => "subagent",
            Self::Plugin => "plugin",
            Self::Sandbox => "sandbox",
            Self::MemoryStore => "memory-store",
            Self::Model => "model",
            Self::Intervention => "intervention",
            Self::AgentConfig => "agent-config",
        }
    }
}

/// The language a module reference is written in (`HarnessModuleLanguage`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum HarnessModuleLanguage {
    /// `typescript`.
    Typescript,
    /// `python`.
    Python,
}

impl HarnessModuleLanguage {
    /// The config spelling.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Typescript => "typescript",
            Self::Python => "python",
        }
    }
}

/// A reference to a value defined in code (`HarnessModuleReference`). Rust parses and keeps
/// these so a config round-trips, but cannot load them yet.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct HarnessModuleReference {
    /// What the module provides.
    pub kind: HarnessModuleKind,
    /// The module specifier: a path or a package name.
    pub module: String,
    /// The export to read; `default` when `None`.
    pub export: Option<String>,
    /// The language the module is written in.
    pub language: Option<HarnessModuleLanguage>,
    /// Source files the module depends on.
    pub files: Option<Vec<String>>,
    /// The package the module comes from.
    pub dependency: Option<String>,
    /// Keys the reference carries beyond the known ones, kept as written.
    pub extra: JsonObject,
}

impl HarnessModuleReference {
    /// A reference of `kind` to `module`.
    pub fn new(kind: HarnessModuleKind, module: impl Into<String>) -> Self {
        Self {
            kind,
            module: module.into(),
            export: None,
            language: None,
            files: None,
            dependency: None,
            extra: JsonObject::new(),
        }
    }

    /// Sets the export to read.
    #[must_use]
    pub fn with_export(mut self, export: impl Into<String>) -> Self {
        self.export = Some(export.into());
        self
    }

    /// Sets the module's language.
    #[must_use]
    pub fn with_language(mut self, language: HarnessModuleLanguage) -> Self {
        self.language = Some(language);
        self
    }
}

impl Serialize for HarnessModuleReference {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serializer.serialize_map(None)?;
        map.serialize_entry("kind", &self.kind)?;
        map.serialize_entry("module", &self.module)?;
        if let Some(export) = &self.export {
            map.serialize_entry("export", export)?;
        }
        if let Some(language) = &self.language {
            map.serialize_entry("language", language)?;
        }
        if let Some(files) = &self.files {
            map.serialize_entry("files", files)?;
        }
        if let Some(dependency) = &self.dependency {
            map.serialize_entry("dependency", dependency)?;
        }
        for (key, value) in &self.extra {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }
}

/// The summarizer model in a portable `web_fetch` config: a `"provider/name"` string or a model
/// module reference.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum HarnessConfigModel {
    /// A `"provider/name"` string.
    Name(String),
    /// A model module reference.
    Module(HarnessModuleReference),
}

impl Serialize for HarnessConfigModel {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Name(name) => serializer.serialize_str(name),
            Self::Module(reference) => reference.serialize(serializer),
        }
    }
}

/// JSON form of [`WebFetchConfig`](crate::WebFetchConfig) (`HarnessConfigWebFetch`).
#[derive(Debug, Clone, Default, PartialEq, Serialize, bon::Builder)]
pub struct HarnessConfigWebFetch {
    /// The summarizer model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<HarnessConfigModel>,
    /// How the request is made.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transport: Option<WebFetchTransport>,
}

/// JSON form of [`BuiltinToolsConfig`](crate::BuiltinToolsConfig) (`HarnessConfigBuiltinTools`),
/// with `web_fetch.model` allowed to be a module reference.
#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
pub struct HarnessConfigBuiltinTools {
    /// The `"*"` key.
    pub all: Option<bool>,
    /// `shell`.
    pub shell: Option<Setting<ShellConfig>>,
    /// `read`.
    pub read: Option<Setting<ReadConfig>>,
    /// `write`.
    pub write: Option<bool>,
    /// `edit`.
    pub edit: Option<bool>,
    /// `web_fetch`.
    pub web_fetch: Option<Setting<HarnessConfigWebFetch>>,
    /// `web_search`.
    pub web_search: Option<WebSearchSetting>,
    /// `programmatic_tool_caller`.
    pub programmatic_tool_caller: Option<Setting<ProgrammaticToolCallerConfig>>,
    /// `subagent`.
    pub subagent: Option<Setting<SubagentConfig>>,
}

impl Serialize for HarnessConfigBuiltinTools {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serializer.serialize_map(None)?;
        if let Some(all) = self.all {
            map.serialize_entry("*", &all)?;
        }
        if let Some(shell) = &self.shell {
            map.serialize_entry("shell", shell)?;
        }
        if let Some(read) = &self.read {
            map.serialize_entry("read", read)?;
        }
        if let Some(write) = self.write {
            map.serialize_entry("write", &write)?;
        }
        if let Some(edit) = self.edit {
            map.serialize_entry("edit", &edit)?;
        }
        if let Some(web_fetch) = &self.web_fetch {
            map.serialize_entry("web_fetch", web_fetch)?;
        }
        if let Some(web_search) = self.web_search {
            map.serialize_entry("web_search", &web_search)?;
        }
        if let Some(caller) = &self.programmatic_tool_caller {
            map.serialize_entry("programmatic_tool_caller", caller)?;
        }
        if let Some(subagent) = &self.subagent {
            map.serialize_entry("subagent", subagent)?;
        }
        map.end()
    }
}

/// The portable `builtinTools`: a list of names or a mapping.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant, reason = "a config value, built once")]
pub enum HarnessConfigBuiltinToolsOption {
    /// Exactly these tools.
    List(Vec<BuiltinToolName>),
    /// Edits to the defaults.
    Mapping(HarnessConfigBuiltinTools),
}

impl Serialize for HarnessConfigBuiltinToolsOption {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::List(names) => names.serialize(serializer),
            Self::Mapping(mapping) => mapping.serialize(serializer),
        }
    }
}

/// The portable `contextManager` (`HarnessConfigContextManager`: `"auto"`, `"agentic"` or
/// `false`; JSON also accepts `"off"` for `false`).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HarnessConfigContextManager {
    /// `'auto'` (the default).
    #[default]
    Auto,
    /// `'agentic'`.
    Agentic,
    /// `false`.
    Off,
}

impl Serialize for HarnessConfigContextManager {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Auto => serializer.serialize_str("auto"),
            Self::Agentic => serializer.serialize_str("agentic"),
            Self::Off => serializer.serialize_bool(false),
        }
    }
}

/// JSON form of [`MemoryConfig`](crate::MemoryConfig) (`HarnessConfigMemory`); stores are
/// declared separately as `memoryStores` module references.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
#[builder(on(String, into))]
pub struct HarnessConfigMemory {
    /// Directory the default file store's files live in.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dir: Option<String>,
}

/// The portable `mcpServers`: a path to a JSON file, or the server map itself.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum HarnessConfigMcpServers {
    /// A path to a JSON file, resolved against the project root.
    Path(String),
    /// The server map, by name.
    Servers(JsonObject),
}

impl Default for HarnessConfigMcpServers {
    fn default() -> Self {
        Self::Servers(JsonObject::new())
    }
}

impl Serialize for HarnessConfigMcpServers {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Path(path) => serializer.serialize_str(path),
            Self::Servers(servers) => servers.serialize(serializer),
        }
    }
}

/// The portable `skills`: on or off, one source, or a list of sources.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HarnessConfigSkills {
    /// `true` scans the default directory; `false` disables skills.
    Enabled(bool),
    /// One directory path or `https://` URL.
    Source(String),
    /// Several sources.
    Sources(Vec<String>),
}

impl Serialize for HarnessConfigSkills {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Enabled(enabled) => serializer.serialize_bool(*enabled),
            Self::Source(source) => serializer.serialize_str(source),
            Self::Sources(sources) => sources.serialize(serializer),
        }
    }
}

/// The portable `interventions`: one value or a list (presets, `.cedar` paths, policies).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HarnessConfigInterventions {
    /// One value.
    One(String),
    /// Several values, layered in order.
    Many(Vec<String>),
}

impl Serialize for HarnessConfigInterventions {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::One(value) => serializer.serialize_str(value),
            Self::Many(values) => values.serialize(serializer),
        }
    }
}

/// Key-value pairs serialized as a JSON object in their order.
fn pairs_as_map<S: Serializer, T: Serialize>(pairs: &[(String, T)], serializer: S) -> Result<S::Ok, S::Error> {
    serializer.collect_map(pairs.iter().map(|(key, value)| (key, value)))
}

/// Dependencies a project declares for the runtimes that load its modules (`HarnessDependencies`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
pub struct HarnessDependencies {
    /// npm package names to version ranges, in declaration order; serialized as a JSON object.
    #[serde(serialize_with = "pairs_as_map")]
    #[builder(default, with = |packages: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>| {
        packages.into_iter().map(|(name, version)| (name.into(), version.into())).collect()
    })]
    pub typescript: Vec<(String, String)>,
    /// pip requirement lines.
    #[builder(default, with = |lines: impl IntoIterator<Item = impl Into<String>>| lines.into_iter().map(Into::into).collect())]
    pub python: Vec<String>,
}

/// The complete portable form of a harness definition (`HarnessAgentConfig`).
///
/// Parse one from JSON with `serde_json`, which applies the config validation and its messages;
/// unknown keys are dropped with a warning.
///
/// ```
/// use strands_harness::{Effort, HarnessAgentConfig};
///
/// let config: HarnessAgentConfig = serde_json::from_value(serde_json::json!({
///     "name": "Reviewer",
///     "effort": "high",
///     "builtinTools": ["read", "shell"],
/// }))?;
/// assert_eq!(config.effort, Effort::High);
/// assert_eq!(config.model, "bedrock/global.anthropic.claude-opus-5-5");
/// # Ok::<(), serde_json::Error>(())
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, bon::Builder)]
#[serde(rename_all = "camelCase", try_from = "serde_json::Value")]
#[builder(on(String, into))]
pub struct HarnessAgentConfig {
    /// The agent's name.
    #[builder(default = "Strands harness".to_owned())]
    pub name: String,
    /// What the agent does.
    #[builder(default)]
    pub description: String,
    /// A domain block appended after the harness contract.
    #[builder(default)]
    pub instructions: String,
    /// A `"provider/name"` model string.
    #[builder(default = DEFAULT_MODEL.to_owned())]
    pub model: String,
    /// A model defined in code, replacing `model`.
    pub model_module: Option<HarnessModuleReference>,
    /// Reasoning effort.
    #[builder(default = DEFAULT_EFFORT)]
    pub effort: Effort,
    /// Tools defined in code.
    #[builder(default)]
    pub tools: Vec<HarnessModuleReference>,
    /// Specialist agents defined in code, each exposed as a tool.
    #[builder(default)]
    pub subagents: Vec<HarnessModuleReference>,
    /// MCP servers to connect.
    #[builder(default)]
    pub mcp_servers: HarnessConfigMcpServers,
    /// Which built-in tools to enable.
    #[builder(default = HarnessConfigBuiltinToolsOption::List(DEFAULT_BUILTIN_TOOLS.to_vec()))]
    pub builtin_tools: HarnessConfigBuiltinToolsOption,
    /// Prompt caching.
    #[builder(default = true)]
    pub caching: bool,
    /// SDK context management.
    #[builder(default = HarnessConfigContextManager::Auto)]
    pub context_manager: HarnessConfigContextManager,
    /// Session persistence.
    #[builder(default = Setting::Enabled(true))]
    pub session: Setting<SessionConfig>,
    /// Agent Skills.
    #[builder(default = HarnessConfigSkills::Enabled(true))]
    pub skills: HarnessConfigSkills,
    /// Long-term memory.
    #[builder(default = Setting::Enabled(true))]
    pub memory: Setting<HarnessConfigMemory>,
    /// Memory stores defined in code.
    #[builder(default)]
    pub memory_stores: Vec<HarnessModuleReference>,
    /// Plugins defined in code.
    #[builder(default)]
    pub plugins: Vec<HarnessModuleReference>,
    /// Built-in feature plugins.
    #[builder(default = DEFAULT_BUILTIN_PLUGINS.to_vec())]
    pub builtin_plugins: Vec<BuiltinPluginName>,
    /// Tool-call interventions.
    pub interventions: Option<HarnessConfigInterventions>,
    /// Intervention handlers defined in code.
    #[builder(default)]
    pub intervention_modules: Vec<HarnessModuleReference>,
    /// A sandbox defined in code.
    pub sandbox: Option<HarnessModuleReference>,
    /// SDK `AgentConfig` values defined in code, by key, in declaration order; serialized as a
    /// JSON object.
    #[serde(serialize_with = "pairs_as_map")]
    #[builder(default)]
    pub agent_config_modules: Vec<(String, HarnessModuleReference)>,
    /// Dependencies of the referenced modules.
    #[builder(default)]
    pub dependencies: HarnessDependencies,
    /// JSON-compatible SDK `AgentConfig` fields the harness does not own.
    #[builder(default)]
    pub agent_config: JsonObject,
}

impl Default for HarnessAgentConfig {
    fn default() -> Self {
        Self::builder().build()
    }
}

impl TryFrom<serde_json::Value> for HarnessAgentConfig {
    type Error = String;

    fn try_from(value: serde_json::Value) -> Result<Self, String> {
        normalize_harness_agent_config(&value).map_err(|error| error.to_string())
    }
}

/// The defaults every key of a portable config falls back to (`DEFAULT_HARNESS_AGENT_CONFIG`).
pub static DEFAULT_HARNESS_AGENT_CONFIG: LazyLock<HarnessAgentConfig> = LazyLock::new(HarnessAgentConfig::default);

/// Validates a config built in code the way a parsed one is.
///
/// ```
/// use strands_harness::{HarnessAgentConfig, define_harness_agent_config};
///
/// let config = define_harness_agent_config(HarnessAgentConfig {
///     name: "Reviewer".into(),
///     ..Default::default()
/// })?;
/// assert_eq!(config.name, "Reviewer");
/// assert!(define_harness_agent_config(HarnessAgentConfig { name: String::new(), ..Default::default() }).is_err());
/// # Ok::<(), strands::Error>(())
/// ```
///
/// # Errors
///
/// The validation messages, such as `Invalid agent config:\nname must be a non-empty string.`
pub fn define_harness_agent_config(config: HarnessAgentConfig) -> strands::Result<HarnessAgentConfig> {
    let value = serde_json::to_value(config).map_err(strands::Error::other)?;
    normalize_harness_agent_config(&value)
}

#[cfg(test)]
mod tests;