Skip to main content

harn_cli/package/
manifest.rs

1use super::errors::PackageError;
2use super::*;
3pub use harn_modules::personas::{
4    PersonaAutonomyTier, PersonaManifestEntry, PersonaValidationError, ResolvedPersonaManifest,
5};
6
7#[derive(Debug, Clone, Deserialize)]
8pub struct Manifest {
9    pub package: Option<PackageInfo>,
10    #[serde(default)]
11    pub dependencies: HashMap<String, Dependency>,
12    #[serde(default)]
13    pub mcp: Vec<McpServerConfig>,
14    #[serde(default)]
15    pub check: CheckConfig,
16    #[serde(default)]
17    pub workspace: WorkspaceConfig,
18    /// `[registry]` table — lightweight package discovery index
19    /// configuration. The CLI also honors `HARN_PACKAGE_REGISTRY` and
20    /// `--registry` flags for one-off overrides.
21    #[serde(default)]
22    pub registry: PackageRegistryConfig,
23    /// `[skills]` table — per-project skill discovery configuration
24    /// (paths, lookup_order, disable).
25    #[serde(default)]
26    pub skills: SkillsConfig,
27    /// `[[skill.source]]` array-of-tables — declared skill sources
28    /// (filesystem, git, reserved registry).
29    #[serde(default)]
30    pub skill: SkillTables,
31    /// `[capabilities]` section — per-provider-per-model override of
32    /// the shipped capability matrix (`defer_loading`, `tool_search`,
33    /// `prompt_caching`, etc.). Entries under `[[capabilities.provider.<name>]]`
34    /// are prepended to the built-in rules for the same provider so
35    /// early adopters can flag proxied endpoints as supporting tool
36    /// search without waiting for a Harn release. See
37    /// `harn_vm::llm::capabilities` for the rule schema.
38    #[serde(default)]
39    pub capabilities: Option<harn_vm::llm::capabilities::CapabilitiesFile>,
40    /// Stable exported package modules. Keys are the logical import
41    /// suffixes (e.g. `providers/openai`) and values are package-root-
42    /// relative file paths. Consumers import them via `<package>/<key>`.
43    #[serde(default)]
44    pub exports: HashMap<String, String>,
45    /// `[llm]` section — packaged provider definitions, aliases,
46    /// inference rules, tier rules, and model defaults. Uses the same
47    /// schema as `providers.toml`, but merges into the current run
48    /// instead of replacing the global config file.
49    #[serde(default)]
50    pub llm: harn_vm::llm_config::ProvidersConfig,
51    /// `[[hooks]]` array-of-tables — declarative runtime hooks installed
52    /// once per process/thread before execution starts. Matches the
53    /// manifest-extension ABI shape added by `[exports]` / `[llm]`, but
54    /// the handlers themselves live in Harn modules.
55    #[serde(default)]
56    pub hooks: Vec<HookConfig>,
57    /// `[[triggers]]` array-of-tables — declarative event-driven trigger
58    /// registrations that resolve local handlers and predicates from Harn
59    /// modules at load time and preserve remote URI schemes for later
60    /// dispatcher work.
61    #[serde(default)]
62    pub triggers: Vec<TriggerManifestEntry>,
63    /// `[[handoff_routes]]` array-of-tables — declarative handoff route data.
64    /// Route selection stays in Harn stdlib/persona code; the Rust manifest
65    /// loader makes these tenant routes available to that code.
66    #[serde(default)]
67    pub handoff_routes: Vec<harn_vm::HandoffRouteConfig>,
68    /// `[[providers]]` array-of-tables — provider-specific connector
69    /// overrides used by the orchestrator to load either builtin Rust
70    /// connectors or `.harn` modules as connector implementations.
71    #[serde(default)]
72    pub providers: Vec<ProviderManifestEntry>,
73    /// `[[personas]]` array-of-tables — durable, non-executing agent role
74    /// manifests. Personas bind an entry workflow to tools, capabilities,
75    /// autonomy, budgets, receipts, handoffs, evals, and rollout metadata.
76    #[serde(default)]
77    pub personas: Vec<PersonaManifestEntry>,
78    /// `[connector_contract]` table — deterministic package-local fixtures
79    /// consumed by `harn connector check` for pure-Harn connector packages.
80    #[serde(default, alias = "connector-contract")]
81    pub connector_contract: ConnectorContractConfig,
82    /// `[orchestrator]` table — listener-level controls shared by
83    /// manifest-driven ingress surfaces.
84    #[serde(default)]
85    pub orchestrator: OrchestratorConfig,
86}
87
88#[derive(Debug, Clone, Default, Deserialize)]
89pub struct OrchestratorConfig {
90    #[serde(default, alias = "allowed-origins")]
91    pub allowed_origins: Vec<String>,
92    #[serde(default, alias = "max-body-bytes")]
93    pub max_body_bytes: Option<usize>,
94    #[serde(default)]
95    pub budget: OrchestratorBudgetSpec,
96    #[serde(default)]
97    pub drain: OrchestratorDrainConfig,
98    #[serde(default)]
99    pub pumps: OrchestratorPumpConfig,
100}
101
102#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
103pub struct OrchestratorBudgetSpec {
104    #[serde(default)]
105    pub daily_cost_usd: Option<f64>,
106    #[serde(default)]
107    pub hourly_cost_usd: Option<f64>,
108}
109
110#[derive(Debug, Clone, Deserialize)]
111pub struct OrchestratorDrainConfig {
112    #[serde(default = "default_orchestrator_drain_max_items", alias = "max-items")]
113    pub max_items: usize,
114    #[serde(
115        default = "default_orchestrator_drain_deadline_seconds",
116        alias = "deadline-seconds"
117    )]
118    pub deadline_seconds: u64,
119}
120
121impl Default for OrchestratorDrainConfig {
122    fn default() -> Self {
123        Self {
124            max_items: default_orchestrator_drain_max_items(),
125            deadline_seconds: default_orchestrator_drain_deadline_seconds(),
126        }
127    }
128}
129
130pub(crate) fn default_orchestrator_drain_max_items() -> usize {
131    1024
132}
133
134pub(crate) fn default_orchestrator_drain_deadline_seconds() -> u64 {
135    30
136}
137
138#[derive(Debug, Clone, Deserialize)]
139pub struct OrchestratorPumpConfig {
140    #[serde(
141        default = "default_orchestrator_pump_max_outstanding",
142        alias = "max-outstanding"
143    )]
144    pub max_outstanding: usize,
145}
146
147impl Default for OrchestratorPumpConfig {
148    fn default() -> Self {
149        Self {
150            max_outstanding: default_orchestrator_pump_max_outstanding(),
151        }
152    }
153}
154
155pub(crate) fn default_orchestrator_pump_max_outstanding() -> usize {
156    64
157}
158
159#[derive(Debug, Clone, Deserialize)]
160pub struct HookConfig {
161    pub event: harn_vm::orchestration::HookEvent,
162    #[serde(default = "default_hook_pattern")]
163    pub pattern: String,
164    pub handler: String,
165}
166
167pub(crate) fn default_hook_pattern() -> String {
168    "*".to_string()
169}
170
171#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
172pub struct TriggerManifestEntry {
173    pub id: String,
174    #[serde(default)]
175    pub kind: Option<TriggerKind>,
176    #[serde(default)]
177    pub provider: Option<harn_vm::ProviderId>,
178    #[serde(default, alias = "tier")]
179    pub autonomy_tier: harn_vm::AutonomyTier,
180    #[serde(default, rename = "match")]
181    pub match_: Option<TriggerMatchExpr>,
182    #[serde(default)]
183    pub sources: Vec<TriggerSourceManifestEntry>,
184    #[serde(default)]
185    pub when: Option<String>,
186    #[serde(default)]
187    pub when_budget: Option<TriggerWhenBudgetSpec>,
188    pub handler: String,
189    #[serde(default)]
190    pub dedupe_key: Option<String>,
191    #[serde(default)]
192    pub retry: TriggerRetrySpec,
193    #[serde(default)]
194    pub priority: Option<TriggerPriorityField>,
195    #[serde(default)]
196    pub budget: TriggerBudgetSpec,
197    #[serde(default)]
198    pub concurrency: Option<TriggerConcurrencyManifestSpec>,
199    #[serde(default)]
200    pub throttle: Option<TriggerThrottleManifestSpec>,
201    #[serde(default)]
202    pub rate_limit: Option<TriggerRateLimitManifestSpec>,
203    #[serde(default)]
204    pub debounce: Option<TriggerDebounceManifestSpec>,
205    #[serde(default)]
206    pub singleton: Option<TriggerSingletonManifestSpec>,
207    #[serde(default)]
208    pub batch: Option<TriggerBatchManifestSpec>,
209    #[serde(default)]
210    pub window: Option<TriggerStreamWindowManifestSpec>,
211    #[serde(default, alias = "dlq-alerts")]
212    pub dlq_alerts: Vec<TriggerDlqAlertManifestSpec>,
213    #[serde(default)]
214    pub secrets: BTreeMap<String, String>,
215    #[serde(default)]
216    pub filter: Option<String>,
217    #[serde(flatten, default)]
218    pub kind_specific: BTreeMap<String, toml::Value>,
219}
220
221#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
222pub struct TriggerSourceManifestEntry {
223    #[serde(default)]
224    pub id: Option<String>,
225    pub kind: TriggerKind,
226    pub provider: harn_vm::ProviderId,
227    #[serde(default, rename = "match")]
228    pub match_: Option<TriggerMatchExpr>,
229    #[serde(default)]
230    pub dedupe_key: Option<String>,
231    #[serde(default)]
232    pub retry: Option<TriggerRetrySpec>,
233    #[serde(default)]
234    pub priority: Option<TriggerPriorityField>,
235    #[serde(default)]
236    pub budget: Option<TriggerBudgetSpec>,
237    #[serde(default)]
238    pub concurrency: Option<TriggerConcurrencyManifestSpec>,
239    #[serde(default)]
240    pub throttle: Option<TriggerThrottleManifestSpec>,
241    #[serde(default)]
242    pub rate_limit: Option<TriggerRateLimitManifestSpec>,
243    #[serde(default)]
244    pub debounce: Option<TriggerDebounceManifestSpec>,
245    #[serde(default)]
246    pub singleton: Option<TriggerSingletonManifestSpec>,
247    #[serde(default)]
248    pub batch: Option<TriggerBatchManifestSpec>,
249    #[serde(default)]
250    pub window: Option<TriggerStreamWindowManifestSpec>,
251    #[serde(default)]
252    pub secrets: BTreeMap<String, String>,
253    #[serde(default)]
254    pub filter: Option<String>,
255    #[serde(flatten, default)]
256    pub kind_specific: BTreeMap<String, toml::Value>,
257}
258
259#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
260#[serde(rename_all = "kebab-case")]
261pub enum TriggerKind {
262    Webhook,
263    Cron,
264    Poll,
265    Stream,
266    Predicate,
267    A2aPush,
268}
269
270#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
271pub struct TriggerMatchExpr {
272    #[serde(default)]
273    pub events: Vec<String>,
274    #[serde(flatten, default)]
275    pub extra: BTreeMap<String, toml::Value>,
276}
277
278#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
279pub struct TriggerRetrySpec {
280    #[serde(default)]
281    pub max: u32,
282    #[serde(default)]
283    pub backoff: TriggerRetryBackoff,
284    #[serde(default = "default_trigger_retention_days")]
285    pub retention_days: u32,
286}
287
288#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
289#[serde(rename_all = "kebab-case")]
290pub enum TriggerRetryBackoff {
291    #[default]
292    Immediate,
293    Svix,
294}
295
296pub(crate) fn default_trigger_retention_days() -> u32 {
297    harn_vm::DEFAULT_INBOX_RETENTION_DAYS
298}
299
300impl Default for TriggerRetrySpec {
301    fn default() -> Self {
302        Self {
303            max: 0,
304            backoff: TriggerRetryBackoff::default(),
305            retention_days: default_trigger_retention_days(),
306        }
307    }
308}
309
310#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
311#[serde(rename_all = "lowercase")]
312pub enum TriggerDispatchPriority {
313    High,
314    #[default]
315    Normal,
316    Low,
317}
318
319#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
320#[serde(untagged)]
321pub enum TriggerPriorityField {
322    Dispatch(TriggerDispatchPriority),
323    Flow(TriggerPriorityManifestSpec),
324}
325
326#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
327pub struct TriggerBudgetSpec {
328    #[serde(default)]
329    pub max_cost_usd: Option<f64>,
330    #[serde(default, alias = "tokens_max")]
331    pub max_tokens: Option<u64>,
332    #[serde(default)]
333    pub daily_cost_usd: Option<f64>,
334    #[serde(default)]
335    pub hourly_cost_usd: Option<f64>,
336    #[serde(default)]
337    pub max_autonomous_decisions_per_hour: Option<u64>,
338    #[serde(default)]
339    pub max_autonomous_decisions_per_day: Option<u64>,
340    #[serde(default)]
341    pub max_concurrent: Option<u32>,
342    #[serde(default)]
343    pub on_budget_exhausted: harn_vm::TriggerBudgetExhaustionStrategy,
344}
345
346#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
347pub struct TriggerWhenBudgetSpec {
348    #[serde(default)]
349    pub max_cost_usd: Option<f64>,
350    #[serde(default)]
351    pub tokens_max: Option<u64>,
352    #[serde(default)]
353    pub timeout: Option<String>,
354}
355
356#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
357pub struct TriggerConcurrencyManifestSpec {
358    #[serde(default)]
359    pub key: Option<String>,
360    pub max: u32,
361}
362
363#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
364pub struct TriggerThrottleManifestSpec {
365    #[serde(default)]
366    pub key: Option<String>,
367    pub period: String,
368    pub max: u32,
369}
370
371#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
372pub struct TriggerRateLimitManifestSpec {
373    #[serde(default)]
374    pub key: Option<String>,
375    pub period: String,
376    pub max: u32,
377}
378
379#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
380pub struct TriggerDebounceManifestSpec {
381    pub key: String,
382    pub period: String,
383}
384
385#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
386pub struct TriggerSingletonManifestSpec {
387    #[serde(default)]
388    pub key: Option<String>,
389}
390
391#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
392pub struct TriggerBatchManifestSpec {
393    #[serde(default)]
394    pub key: Option<String>,
395    pub size: u32,
396    pub timeout: String,
397}
398
399#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
400pub struct TriggerPriorityManifestSpec {
401    pub key: String,
402    #[serde(default)]
403    pub order: Vec<String>,
404}
405
406#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
407#[serde(rename_all = "kebab-case")]
408pub enum TriggerStreamWindowMode {
409    Tumbling,
410    Sliding,
411    Session,
412}
413
414#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
415pub struct TriggerStreamWindowManifestSpec {
416    pub mode: TriggerStreamWindowMode,
417    #[serde(default)]
418    pub key: Option<String>,
419    #[serde(default)]
420    pub size: Option<String>,
421    #[serde(default)]
422    pub every: Option<String>,
423    #[serde(default)]
424    pub gap: Option<String>,
425    #[serde(default)]
426    pub max_items: Option<u32>,
427}
428
429#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
430pub struct TriggerDlqAlertManifestSpec {
431    #[serde(default)]
432    pub destinations: Vec<TriggerDlqAlertDestination>,
433    #[serde(default)]
434    pub threshold: TriggerDlqAlertThreshold,
435}
436
437#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
438pub struct TriggerDlqAlertThreshold {
439    #[serde(default, alias = "entries-in-1h")]
440    pub entries_in_1h: Option<u32>,
441    #[serde(default, alias = "percent-of-dispatches")]
442    pub percent_of_dispatches: Option<f64>,
443}
444
445#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
446#[serde(tag = "kind", rename_all = "snake_case")]
447pub enum TriggerDlqAlertDestination {
448    Slack {
449        channel: String,
450        #[serde(default)]
451        webhook_url_env: Option<String>,
452    },
453    Email {
454        address: String,
455    },
456    Webhook {
457        url: String,
458        #[serde(default)]
459        headers: BTreeMap<String, String>,
460    },
461}
462
463impl TriggerDlqAlertDestination {
464    pub fn label(&self) -> String {
465        match self {
466            Self::Slack { channel, .. } => format!("slack:{channel}"),
467            Self::Email { address } => format!("email:{address}"),
468            Self::Webhook { url, .. } => format!("webhook:{url}"),
469        }
470    }
471}
472
473#[derive(Debug, Clone, PartialEq, Eq)]
474pub enum TriggerHandlerUri {
475    Local(TriggerFunctionRef),
476    A2a {
477        target: String,
478        allow_cleartext: bool,
479    },
480    Worker {
481        queue: String,
482    },
483    Persona {
484        name: String,
485    },
486}
487
488#[derive(Debug, Clone, PartialEq, Eq)]
489pub struct TriggerFunctionRef {
490    pub raw: String,
491    pub module_name: Option<String>,
492    pub function_name: String,
493}
494
495/// `[skills]` table body.
496#[derive(Debug, Default, Clone, Deserialize)]
497#[allow(dead_code)] // `defaults` is parsed per harn#73; default application remains staged.
498pub struct SkillsConfig {
499    /// Additional filesystem roots to scan. Each entry may be a
500    /// literal directory or a glob (`packages/*/skills`). Resolved
501    /// relative to the directory holding harn.toml.
502    #[serde(default)]
503    pub paths: Vec<String>,
504    /// Override priority order. Values are layer labels —
505    /// `cli`, `env`, `project`, `manifest`, `user`, `package`,
506    /// `system`, `host`. Unlisted layers fall through to default
507    /// priority after listed ones.
508    #[serde(default)]
509    pub lookup_order: Vec<String>,
510    /// Disable entire layers. Same label set as `lookup_order`.
511    #[serde(default)]
512    pub disable: Vec<String>,
513    /// Optional remote registry base URL used to resolve
514    /// `<fingerprint>.pub` when a signer is not installed locally.
515    #[serde(default)]
516    pub signer_registry_url: Option<String>,
517    /// `[skills.defaults]` inline sub-table — applied to every
518    /// discovered skill when the field is unset in its SKILL.md
519    /// frontmatter.
520    #[serde(default)]
521    pub defaults: SkillDefaults,
522}
523
524#[derive(Debug, Default, Clone, Deserialize)]
525#[allow(dead_code)] // Parsed per harn#73; loader default application is still staged.
526pub struct SkillDefaults {
527    #[serde(default)]
528    pub tool_search: Option<String>,
529    #[serde(default)]
530    pub always_loaded: Vec<String>,
531}
532
533/// Container for `[[skill.source]]` array-of-tables.
534#[derive(Debug, Default, Clone, Deserialize)]
535pub struct SkillTables {
536    #[serde(default, rename = "source")]
537    pub sources: Vec<SkillSourceEntry>,
538}
539
540/// One `[[skill.source]]` entry. The `registry` variant is accepted
541/// for forward-compat but inert — see issue #73 and `docs/src/skills.md`
542/// for the marketplace timeline.
543#[derive(Debug, Clone, Deserialize)]
544#[serde(tag = "type", rename_all = "lowercase")]
545#[allow(dead_code)] // Git/registry skill sources are manifest-reserved by harn#73.
546pub enum SkillSourceEntry {
547    Fs {
548        path: String,
549        #[serde(default)]
550        namespace: Option<String>,
551    },
552    Git {
553        url: String,
554        #[serde(default)]
555        tag: Option<String>,
556        #[serde(default)]
557        namespace: Option<String>,
558    },
559    Registry {
560        #[serde(default)]
561        url: Option<String>,
562        #[serde(default)]
563        name: Option<String>,
564    },
565}
566
567/// Severity override for preflight diagnostics. `error` (default) fails
568/// `harn check`; `warning` reports but does not fail; `off` suppresses
569/// entirely. Accepted via `[check].preflight_severity` in harn.toml so
570/// repos with hosts that do not expose every capability statically can
571/// keep the checker running on genuine type errors.
572#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
573pub enum PreflightSeverity {
574    #[default]
575    Error,
576    Warning,
577    Off,
578}
579
580impl PreflightSeverity {
581    pub fn from_opt(raw: Option<&str>) -> Self {
582        match raw.map(|s| s.to_ascii_lowercase()) {
583            Some(v) if v == "warning" || v == "warn" => Self::Warning,
584            Some(v) if v == "off" || v == "allow" || v == "silent" => Self::Off,
585            _ => Self::Error,
586        }
587    }
588}
589
590#[derive(Debug, Default, Clone, Deserialize)]
591pub struct CheckConfig {
592    #[serde(default)]
593    pub strict: bool,
594    #[serde(default)]
595    pub strict_types: bool,
596    #[serde(default)]
597    pub disable_rules: Vec<String>,
598    #[serde(default)]
599    pub host_capabilities: HashMap<String, Vec<String>>,
600    #[serde(default, alias = "host_capabilities_file")]
601    pub host_capabilities_path: Option<String>,
602    #[serde(default)]
603    pub bundle_root: Option<String>,
604    /// Downgrade or suppress preflight diagnostics. See
605    /// [`PreflightSeverity`].
606    #[serde(default, alias = "preflight-severity")]
607    pub preflight_severity: Option<String>,
608    /// List of `"capability.operation"` strings that should be accepted
609    /// by preflight without emitting a diagnostic, even if the operation
610    /// is not in the default or loaded capability manifest.
611    #[serde(default, alias = "preflight-allow")]
612    pub preflight_allow: Vec<String>,
613}
614
615#[derive(Debug, Default, Clone, Deserialize)]
616pub struct WorkspaceConfig {
617    /// Directory or file globs (repo-relative) that `harn check --workspace`
618    /// walks to collect the full pipeline tree in one invocation.
619    #[serde(default)]
620    pub pipelines: Vec<String>,
621}
622
623#[derive(Debug, Default, Clone, Deserialize)]
624pub struct PackageRegistryConfig {
625    /// URL or filesystem path to a TOML package index.
626    #[serde(default)]
627    pub url: Option<String>,
628}
629
630#[derive(Debug, Clone, Deserialize)]
631pub struct McpServerConfig {
632    pub name: String,
633    #[serde(default)]
634    pub transport: Option<String>,
635    #[serde(default)]
636    pub command: String,
637    #[serde(default)]
638    pub args: Vec<String>,
639    #[serde(default)]
640    pub env: HashMap<String, String>,
641    #[serde(default)]
642    pub url: String,
643    #[serde(default)]
644    pub auth_token: Option<String>,
645    #[serde(default)]
646    pub client_id: Option<String>,
647    #[serde(default)]
648    pub client_secret: Option<String>,
649    #[serde(default)]
650    pub scopes: Option<String>,
651    #[serde(default)]
652    pub protocol_version: Option<String>,
653    #[serde(default)]
654    pub proxy_server_name: Option<String>,
655    /// When `true`, the server is NOT booted up-front. It boots on the
656    /// first `mcp_call` or on skill activation that declares it in
657    /// `requires_mcp`. See harn#75.
658    #[serde(default)]
659    pub lazy: bool,
660    /// Optional pointer to a Server Card — either an HTTP(S) URL or a
661    /// local filesystem path. When set, `mcp_server_card("name")` reads
662    /// the card from this source (cached per-process with a TTL).
663    #[serde(default)]
664    pub card: Option<String>,
665    /// How long (milliseconds) to keep a lazy server's process alive
666    /// after its last binder releases. 0 / unset → disconnect
667    /// immediately. Ignored for non-lazy servers.
668    #[serde(default, alias = "keep-alive-ms", alias = "keep_alive")]
669    pub keep_alive_ms: Option<u64>,
670}
671
672#[derive(Debug, Clone, Deserialize)]
673#[allow(dead_code)] // Package metadata feeds authoring/publish validation tracked in harn#471.
674pub struct PackageInfo {
675    pub name: Option<String>,
676    pub version: Option<String>,
677    #[serde(default)]
678    pub evals: Vec<String>,
679    #[serde(default)]
680    pub description: Option<String>,
681    #[serde(default)]
682    pub license: Option<String>,
683    #[serde(default)]
684    pub repository: Option<String>,
685    #[serde(default, alias = "harn_version", alias = "harn_version_range")]
686    pub harn: Option<String>,
687    #[serde(default)]
688    pub docs_url: Option<String>,
689}
690
691#[derive(Debug, Clone, Deserialize)]
692#[serde(untagged)]
693pub enum Dependency {
694    Table(DepTable),
695    Path(String),
696}
697
698#[derive(Debug, Clone, Deserialize)]
699pub struct DepTable {
700    pub git: Option<String>,
701    pub tag: Option<String>,
702    pub rev: Option<String>,
703    pub branch: Option<String>,
704    pub path: Option<String>,
705    pub package: Option<String>,
706}
707
708impl Dependency {
709    pub(crate) fn git_url(&self) -> Option<&str> {
710        match self {
711            Dependency::Table(t) => t.git.as_deref(),
712            Dependency::Path(_) => None,
713        }
714    }
715
716    pub(crate) fn rev(&self) -> Option<&str> {
717        match self {
718            Dependency::Table(t) => t.rev.as_deref().or(t.tag.as_deref()),
719            Dependency::Path(_) => None,
720        }
721    }
722
723    pub(crate) fn branch(&self) -> Option<&str> {
724        match self {
725            Dependency::Table(t) => t.branch.as_deref(),
726            Dependency::Path(_) => None,
727        }
728    }
729
730    pub(crate) fn local_path(&self) -> Option<&str> {
731        match self {
732            Dependency::Table(t) => t.path.as_deref(),
733            Dependency::Path(p) => Some(p.as_str()),
734        }
735    }
736}
737
738pub(crate) fn validate_package_alias(alias: &str) -> Result<(), PackageError> {
739    let valid = !alias.is_empty()
740        && alias != "."
741        && alias != ".."
742        && alias
743            .bytes()
744            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'));
745    if valid {
746        Ok(())
747    } else {
748        Err(PackageError::Validation(format!(
749            "invalid dependency alias {alias:?}; use ASCII letters, numbers, '.', '_' or '-'"
750        )))
751    }
752}
753
754pub(crate) fn toml_string_literal(value: &str) -> Result<String, PackageError> {
755    use std::fmt::Write as _;
756
757    let mut encoded = String::with_capacity(value.len() + 2);
758    encoded.push('"');
759    for ch in value.chars() {
760        match ch {
761            '\u{08}' => encoded.push_str("\\b"),
762            '\t' => encoded.push_str("\\t"),
763            '\n' => encoded.push_str("\\n"),
764            '\u{0C}' => encoded.push_str("\\f"),
765            '\r' => encoded.push_str("\\r"),
766            '"' => encoded.push_str("\\\""),
767            '\\' => encoded.push_str("\\\\"),
768            ch if ch <= '\u{1F}' || ch == '\u{7F}' => {
769                write!(&mut encoded, "\\u{:04X}", ch as u32).map_err(|error| {
770                    PackageError::Manifest(format!("failed to encode TOML string: {error}"))
771                })?;
772            }
773            ch => encoded.push(ch),
774        }
775    }
776    encoded.push('"');
777    Ok(encoded)
778}
779
780#[derive(Debug, Default, Clone)]
781pub struct RuntimeExtensions {
782    pub root_manifest: Option<Manifest>,
783    pub root_manifest_path: Option<PathBuf>,
784    pub root_manifest_dir: Option<PathBuf>,
785    pub llm: Option<harn_vm::llm_config::ProvidersConfig>,
786    pub capabilities: Option<harn_vm::llm::capabilities::CapabilitiesFile>,
787    pub hooks: Vec<ResolvedHookConfig>,
788    pub triggers: Vec<ResolvedTriggerConfig>,
789    pub handoff_routes: Vec<harn_vm::HandoffRouteConfig>,
790    pub provider_connectors: Vec<ResolvedProviderConnectorConfig>,
791}
792
793#[derive(Debug, Clone, Deserialize)]
794pub struct ProviderManifestEntry {
795    pub id: harn_vm::ProviderId,
796    pub connector: ProviderConnectorManifest,
797    #[serde(default)]
798    pub oauth: Option<ProviderOAuthManifest>,
799    #[serde(default)]
800    pub capabilities: ConnectorCapabilities,
801}
802
803#[derive(Debug, Clone, Deserialize)]
804pub struct ProviderConnectorManifest {
805    #[serde(default)]
806    pub harn: Option<String>,
807    #[serde(default)]
808    pub rust: Option<String>,
809}
810
811#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
812pub struct ProviderOAuthManifest {
813    #[serde(default, alias = "auth_url", alias = "authorization-endpoint")]
814    pub authorization_endpoint: Option<String>,
815    #[serde(default, alias = "token_url", alias = "token-endpoint")]
816    pub token_endpoint: Option<String>,
817    #[serde(default, alias = "registration_url", alias = "registration-endpoint")]
818    pub registration_endpoint: Option<String>,
819    #[serde(default)]
820    pub resource: Option<String>,
821    #[serde(default, alias = "scope")]
822    pub scopes: Option<String>,
823    #[serde(default, alias = "client-id")]
824    pub client_id: Option<String>,
825    #[serde(default, alias = "client-secret")]
826    pub client_secret: Option<String>,
827    #[serde(default, alias = "token_auth_method", alias = "token-auth-method")]
828    pub token_endpoint_auth_method: Option<String>,
829}
830
831#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
832pub struct ConnectorCapabilities {
833    pub webhook: bool,
834    pub oauth: bool,
835    pub rate_limit: bool,
836    pub pagination: bool,
837    pub graphql: bool,
838    pub streaming: bool,
839}
840
841impl ConnectorCapabilities {
842    pub const FEATURES: [&'static str; 6] = [
843        "webhook",
844        "oauth",
845        "rate_limit",
846        "pagination",
847        "graphql",
848        "streaming",
849    ];
850
851    fn enable(&mut self, feature: &str) -> Result<(), String> {
852        match normalize_connector_capability(feature).as_str() {
853            "webhook" => self.webhook = true,
854            "oauth" => self.oauth = true,
855            "rate_limit" => self.rate_limit = true,
856            "pagination" => self.pagination = true,
857            "graphql" => self.graphql = true,
858            "streaming" => self.streaming = true,
859            other => {
860                return Err(format!(
861                    "unknown connector capability '{feature}' (normalized as '{other}')"
862                ));
863            }
864        }
865        Ok(())
866    }
867}
868
869#[derive(Debug, Default, Deserialize)]
870struct ConnectorCapabilitiesTable {
871    #[serde(default)]
872    webhook: bool,
873    #[serde(default)]
874    oauth: bool,
875    #[serde(default, alias = "rate-limit")]
876    rate_limit: bool,
877    #[serde(default)]
878    pagination: bool,
879    #[serde(default)]
880    graphql: bool,
881    #[serde(default)]
882    streaming: bool,
883}
884
885impl From<ConnectorCapabilitiesTable> for ConnectorCapabilities {
886    fn from(value: ConnectorCapabilitiesTable) -> Self {
887        Self {
888            webhook: value.webhook,
889            oauth: value.oauth,
890            rate_limit: value.rate_limit,
891            pagination: value.pagination,
892            graphql: value.graphql,
893            streaming: value.streaming,
894        }
895    }
896}
897
898impl<'de> Deserialize<'de> for ConnectorCapabilities {
899    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
900    where
901        D: serde::Deserializer<'de>,
902    {
903        #[derive(Deserialize)]
904        #[serde(untagged)]
905        enum RawConnectorCapabilities {
906            List(Vec<String>),
907            Table(ConnectorCapabilitiesTable),
908        }
909
910        match RawConnectorCapabilities::deserialize(deserializer)? {
911            RawConnectorCapabilities::List(features) => {
912                let mut capabilities = ConnectorCapabilities::default();
913                for feature in features {
914                    capabilities
915                        .enable(&feature)
916                        .map_err(serde::de::Error::custom)?;
917                }
918                Ok(capabilities)
919            }
920            RawConnectorCapabilities::Table(table) => Ok(table.into()),
921        }
922    }
923}
924
925pub fn normalize_connector_capability(feature: &str) -> String {
926    feature.trim().to_lowercase().replace('-', "_")
927}
928
929#[derive(Debug, Clone, Default, Deserialize)]
930pub struct ConnectorContractConfig {
931    #[serde(default)]
932    pub version: Option<u32>,
933    #[serde(default)]
934    pub fixtures: Vec<ConnectorContractFixture>,
935}
936
937#[derive(Debug, Clone, Deserialize)]
938pub struct ConnectorContractFixture {
939    pub provider: harn_vm::ProviderId,
940    #[serde(default)]
941    pub name: Option<String>,
942    #[serde(default)]
943    pub kind: Option<String>,
944    #[serde(default)]
945    pub headers: BTreeMap<String, String>,
946    #[serde(default)]
947    pub query: BTreeMap<String, String>,
948    #[serde(default)]
949    pub metadata: Option<toml::Value>,
950    #[serde(default)]
951    pub body: Option<String>,
952    #[serde(default)]
953    pub body_json: Option<toml::Value>,
954    #[serde(default)]
955    pub expect_type: Option<String>,
956    #[serde(default)]
957    pub expect_kind: Option<String>,
958    #[serde(default)]
959    pub expect_dedupe_key: Option<String>,
960    #[serde(default)]
961    pub expect_signature_state: Option<String>,
962    #[serde(default)]
963    pub expect_payload_contains: Option<toml::Value>,
964    #[serde(default)]
965    pub expect_response_status: Option<u16>,
966    #[serde(default)]
967    pub expect_response_body: Option<toml::Value>,
968    #[serde(default)]
969    pub expect_event_count: Option<usize>,
970    #[serde(default)]
971    pub expect_error_contains: Option<String>,
972}
973
974#[derive(Debug, Clone, PartialEq, Eq)]
975pub enum ResolvedProviderConnectorKind {
976    Harn { module: String },
977    RustBuiltin,
978    Invalid(String),
979}
980
981#[derive(Debug, Clone)]
982pub struct ResolvedProviderConnectorConfig {
983    pub id: harn_vm::ProviderId,
984    pub manifest_dir: PathBuf,
985    pub connector: ResolvedProviderConnectorKind,
986    pub oauth: Option<ProviderOAuthManifest>,
987}
988
989#[derive(Debug, Clone)]
990pub struct ResolvedHookConfig {
991    pub event: harn_vm::orchestration::HookEvent,
992    pub pattern: String,
993    pub handler: String,
994    pub manifest_dir: PathBuf,
995    pub package_name: Option<String>,
996    pub exports: HashMap<String, String>,
997}
998
999#[derive(Debug, Clone)]
1000#[allow(dead_code)] // Trigger metadata is carried forward for harn#156 doctor and harn#159 dispatcher work.
1001pub struct ResolvedTriggerConfig {
1002    pub id: String,
1003    pub kind: TriggerKind,
1004    pub provider: harn_vm::ProviderId,
1005    pub autonomy_tier: harn_vm::AutonomyTier,
1006    pub match_: TriggerMatchExpr,
1007    pub when: Option<String>,
1008    pub when_budget: Option<TriggerWhenBudgetSpec>,
1009    pub handler: String,
1010    pub dedupe_key: Option<String>,
1011    pub retry: TriggerRetrySpec,
1012    pub dispatch_priority: TriggerDispatchPriority,
1013    pub budget: TriggerBudgetSpec,
1014    pub concurrency: Option<TriggerConcurrencyManifestSpec>,
1015    pub throttle: Option<TriggerThrottleManifestSpec>,
1016    pub rate_limit: Option<TriggerRateLimitManifestSpec>,
1017    pub debounce: Option<TriggerDebounceManifestSpec>,
1018    pub singleton: Option<TriggerSingletonManifestSpec>,
1019    pub batch: Option<TriggerBatchManifestSpec>,
1020    pub window: Option<TriggerStreamWindowManifestSpec>,
1021    pub priority_flow: Option<TriggerPriorityManifestSpec>,
1022    pub secrets: BTreeMap<String, String>,
1023    pub filter: Option<String>,
1024    pub kind_specific: BTreeMap<String, toml::Value>,
1025    pub manifest_dir: PathBuf,
1026    pub manifest_path: PathBuf,
1027    pub package_name: Option<String>,
1028    pub exports: HashMap<String, String>,
1029    pub table_index: usize,
1030    pub shape_error: Option<String>,
1031}
1032
1033#[derive(Debug, Clone)]
1034#[allow(dead_code)] // Collected bindings are validated now and consumed by harn#159 dispatcher work.
1035pub struct CollectedManifestTrigger {
1036    pub config: ResolvedTriggerConfig,
1037    pub handler: CollectedTriggerHandler,
1038    pub when: Option<CollectedTriggerPredicate>,
1039    pub flow_control: harn_vm::TriggerFlowControlConfig,
1040}
1041
1042#[derive(Debug, Clone)]
1043#[allow(dead_code)] // Remote targets and closures are retained for harn#159 trigger execution.
1044pub enum CollectedTriggerHandler {
1045    Local {
1046        reference: TriggerFunctionRef,
1047        closure: Rc<harn_vm::VmClosure>,
1048    },
1049    A2a {
1050        target: String,
1051        allow_cleartext: bool,
1052    },
1053    Worker {
1054        queue: String,
1055    },
1056    Persona {
1057        binding: harn_vm::PersonaRuntimeBinding,
1058    },
1059}
1060
1061#[derive(Debug, Clone)]
1062#[allow(dead_code)] // Predicate closures are validated now and reused by harn#161 dispatch gating.
1063pub struct CollectedTriggerPredicate {
1064    pub reference: TriggerFunctionRef,
1065    pub closure: Rc<harn_vm::VmClosure>,
1066}
1067
1068pub(crate) type ManifestModuleCacheKey = (PathBuf, Option<String>, Option<String>);
1069pub(crate) type ManifestModuleExports = BTreeMap<String, Rc<harn_vm::VmClosure>>;
1070
1071static MANIFEST_PROVIDER_SCHEMA_LOCK: OnceLock<tokio::sync::Mutex<()>> = OnceLock::new();
1072
1073pub(crate) async fn lock_manifest_provider_schemas() -> tokio::sync::MutexGuard<'static, ()> {
1074    MANIFEST_PROVIDER_SCHEMA_LOCK
1075        .get_or_init(|| tokio::sync::Mutex::new(()))
1076        .lock()
1077        .await
1078}
1079
1080pub(crate) fn read_manifest_from_path(path: &Path) -> Result<Manifest, PackageError> {
1081    let content = fs::read_to_string(path).map_err(|error| {
1082        if error.kind() == std::io::ErrorKind::NotFound {
1083            PackageError::Manifest(format!(
1084                "No {} found in {}.",
1085                MANIFEST,
1086                path.parent().unwrap_or_else(|| Path::new(".")).display()
1087            ))
1088        } else {
1089            PackageError::Manifest(format!("failed to read {}: {error}", path.display()))
1090        }
1091    })?;
1092    toml::from_str::<Manifest>(&content).map_err(|error| {
1093        PackageError::Manifest(format!("failed to parse {}: {error}", path.display()))
1094    })
1095}
1096
1097pub(crate) fn write_manifest_content(path: &Path, content: &str) -> Result<(), PackageError> {
1098    harn_vm::atomic_io::atomic_write(path, content.as_bytes()).map_err(|error| {
1099        PackageError::Manifest(format!("failed to write {}: {error}", path.display()))
1100    })
1101}
1102
1103pub(crate) fn absolutize_check_config_paths(
1104    mut config: CheckConfig,
1105    manifest_dir: &Path,
1106) -> CheckConfig {
1107    if let Some(path) = config.host_capabilities_path.clone() {
1108        let candidate = PathBuf::from(&path);
1109        if !candidate.is_absolute() {
1110            config.host_capabilities_path =
1111                Some(manifest_dir.join(candidate).display().to_string());
1112        }
1113    }
1114    if let Some(path) = config.bundle_root.clone() {
1115        let candidate = PathBuf::from(&path);
1116        if !candidate.is_absolute() {
1117            config.bundle_root = Some(manifest_dir.join(candidate).display().to_string());
1118        }
1119    }
1120    config
1121}
1122
1123/// Walk upward from `start` (or its parent if it's a file path that
1124/// does not yet exist) looking for the nearest `harn.toml`. Stops at
1125/// a `.git` boundary so a stray manifest in `$HOME` or a parent
1126/// project is never silently picked up. Returns `(manifest, manifest_dir)`
1127/// when found.
1128pub(crate) fn find_nearest_manifest(start: &Path) -> Option<(Manifest, PathBuf)> {
1129    const MAX_PARENT_DIRS: usize = 16;
1130    let base = if start.is_absolute() {
1131        start.to_path_buf()
1132    } else {
1133        std::env::current_dir()
1134            .unwrap_or_else(|_| PathBuf::from("."))
1135            .join(start)
1136    };
1137    let mut cursor: Option<PathBuf> = if base.is_dir() {
1138        Some(base)
1139    } else {
1140        base.parent().map(Path::to_path_buf)
1141    };
1142    let mut steps = 0usize;
1143    while let Some(dir) = cursor {
1144        if steps >= MAX_PARENT_DIRS {
1145            break;
1146        }
1147        steps += 1;
1148        let candidate = dir.join(MANIFEST);
1149        if candidate.is_file() {
1150            match read_manifest_from_path(&candidate) {
1151                Ok(manifest) => return Some((manifest, dir)),
1152                Err(error) => {
1153                    eprintln!("warning: {error}");
1154                    return None;
1155                }
1156            }
1157        }
1158        if dir.join(".git").exists() {
1159            break;
1160        }
1161        cursor = dir.parent().map(Path::to_path_buf);
1162    }
1163    None
1164}
1165
1166/// Load the `[check]` config from the nearest `harn.toml`.
1167/// Walks up from the given file (or from cwd if no file is given),
1168/// stopping at a `.git` boundary.
1169pub fn load_check_config(harn_file: Option<&std::path::Path>) -> CheckConfig {
1170    let anchor = harn_file
1171        .map(Path::to_path_buf)
1172        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1173    if let Some((manifest, dir)) = find_nearest_manifest(&anchor) {
1174        return absolutize_check_config_paths(manifest.check, &dir);
1175    }
1176    CheckConfig::default()
1177}
1178
1179/// Load the `[workspace]` config and the directory of the `harn.toml`
1180/// it came from. Paths in the returned config are left as-is (callers
1181/// resolve them against the returned `manifest_dir`).
1182pub fn load_workspace_config(anchor: Option<&Path>) -> Option<(WorkspaceConfig, PathBuf)> {
1183    let anchor = anchor
1184        .map(Path::to_path_buf)
1185        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1186    let (manifest, dir) = find_nearest_manifest(&anchor)?;
1187    Some((manifest.workspace, dir))
1188}
1189
1190pub fn load_package_eval_pack_paths(anchor: Option<&Path>) -> Result<Vec<PathBuf>, PackageError> {
1191    let anchor = anchor
1192        .map(Path::to_path_buf)
1193        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1194    let Some((manifest, dir)) = find_nearest_manifest(&anchor) else {
1195        return Err(PackageError::Manifest(
1196            "no harn.toml found for package eval discovery".to_string(),
1197        ));
1198    };
1199
1200    let declared = manifest
1201        .package
1202        .as_ref()
1203        .map(|package| package.evals.clone())
1204        .unwrap_or_default();
1205    let mut paths = if declared.is_empty() {
1206        let default_pack = dir.join("harn.eval.toml");
1207        if default_pack.is_file() {
1208            vec![default_pack]
1209        } else {
1210            Vec::new()
1211        }
1212    } else {
1213        declared
1214            .iter()
1215            .map(|entry| {
1216                let path = PathBuf::from(entry);
1217                if path.is_absolute() {
1218                    path
1219                } else {
1220                    dir.join(path)
1221                }
1222            })
1223            .collect()
1224    };
1225    paths.sort();
1226    if paths.is_empty() {
1227        return Err(PackageError::Manifest(
1228            "package declares no eval packs; add [package].evals or harn.eval.toml".to_string(),
1229        ));
1230    }
1231    for path in &paths {
1232        if !path.is_file() {
1233            return Err(PackageError::Manifest(format!(
1234                "eval pack does not exist: {}",
1235                path.display()
1236            )));
1237        }
1238    }
1239    Ok(paths)
1240}
1241
1242#[derive(Debug, Clone)]
1243pub(crate) struct ManifestContext {
1244    pub(crate) manifest: Manifest,
1245    pub(crate) dir: PathBuf,
1246}
1247
1248impl ManifestContext {
1249    pub(crate) fn manifest_path(&self) -> PathBuf {
1250        self.dir.join(MANIFEST)
1251    }
1252
1253    pub(crate) fn lock_path(&self) -> PathBuf {
1254        self.dir.join(LOCK_FILE)
1255    }
1256
1257    pub(crate) fn packages_dir(&self) -> PathBuf {
1258        self.dir.join(PKG_DIR)
1259    }
1260}
1261
1262pub(crate) fn load_current_manifest_context() -> Result<ManifestContext, PackageError> {
1263    let dir = std::env::current_dir()
1264        .map_err(|error| PackageError::Manifest(format!("failed to read cwd: {error}")))?;
1265    let manifest_path = dir.join(MANIFEST);
1266    let manifest = read_manifest_from_path(&manifest_path)?;
1267    Ok(ManifestContext { manifest, dir })
1268}
1269
1270#[cfg(test)]
1271mod tests {
1272    use super::*;
1273
1274    #[test]
1275    fn package_eval_pack_paths_use_package_manifest_entries() {
1276        let tmp = tempfile::tempdir().unwrap();
1277        let root = tmp.path();
1278        fs::create_dir_all(root.join(".git")).unwrap();
1279        fs::create_dir_all(root.join("evals")).unwrap();
1280        fs::write(
1281            root.join(MANIFEST),
1282            r#"
1283    [package]
1284    name = "demo"
1285    version = "0.1.0"
1286    evals = ["evals/webhook.toml"]
1287    "#,
1288        )
1289        .unwrap();
1290        fs::write(
1291            root.join("evals/webhook.toml"),
1292            "version = 1\n[[cases]]\nrun = \"run.json\"\n",
1293        )
1294        .unwrap();
1295
1296        let paths = load_package_eval_pack_paths(Some(&root.join("src/main.harn"))).unwrap();
1297
1298        assert_eq!(paths, vec![root.join("evals/webhook.toml")]);
1299    }
1300    #[test]
1301    fn preflight_severity_parsing_accepts_synonyms() {
1302        assert_eq!(
1303            PreflightSeverity::from_opt(Some("warning")),
1304            PreflightSeverity::Warning
1305        );
1306        assert_eq!(
1307            PreflightSeverity::from_opt(Some("WARN")),
1308            PreflightSeverity::Warning
1309        );
1310        assert_eq!(
1311            PreflightSeverity::from_opt(Some("off")),
1312            PreflightSeverity::Off
1313        );
1314        assert_eq!(
1315            PreflightSeverity::from_opt(Some("allow")),
1316            PreflightSeverity::Off
1317        );
1318        assert_eq!(
1319            PreflightSeverity::from_opt(Some("error")),
1320            PreflightSeverity::Error
1321        );
1322        assert_eq!(PreflightSeverity::from_opt(None), PreflightSeverity::Error);
1323        // Unknown values fall back to the safe default (error).
1324        assert_eq!(
1325            PreflightSeverity::from_opt(Some("bogus")),
1326            PreflightSeverity::Error
1327        );
1328    }
1329
1330    #[test]
1331    fn load_check_config_walks_up_from_nested_file() {
1332        let tmp = tempfile::tempdir().unwrap();
1333        let root = tmp.path();
1334        // Mark root as project boundary so walk-up terminates here.
1335        std::fs::create_dir_all(root.join(".git")).unwrap();
1336        fs::write(
1337            root.join(MANIFEST),
1338            r#"
1339    [check]
1340    preflight_severity = "warning"
1341    preflight_allow = ["custom.scan", "runtime.*"]
1342    host_capabilities_path = "./schemas/host-caps.json"
1343
1344    [workspace]
1345    pipelines = ["pipelines", "scripts"]
1346    "#,
1347        )
1348        .unwrap();
1349        let nested = root.join("src").join("deep");
1350        std::fs::create_dir_all(&nested).unwrap();
1351        let harn_file = nested.join("pipeline.harn");
1352        fs::write(&harn_file, "pipeline main() {}\n").unwrap();
1353
1354        let cfg = load_check_config(Some(&harn_file));
1355        assert_eq!(cfg.preflight_severity.as_deref(), Some("warning"));
1356        assert_eq!(cfg.preflight_allow, vec!["custom.scan", "runtime.*"]);
1357        let caps_path = cfg.host_capabilities_path.expect("host caps path");
1358        assert!(
1359            caps_path.ends_with("schemas/host-caps.json")
1360                || caps_path.ends_with("schemas\\host-caps.json"),
1361            "unexpected absolutized path: {caps_path}"
1362        );
1363
1364        let (workspace, manifest_dir) =
1365            load_workspace_config(Some(&harn_file)).expect("workspace manifest");
1366        assert_eq!(workspace.pipelines, vec!["pipelines", "scripts"]);
1367        // Walk-up lands on the directory containing the harn.toml.
1368        assert_eq!(manifest_dir, root);
1369    }
1370
1371    #[test]
1372    fn orchestrator_drain_config_parses_defaults_and_overrides() {
1373        let default_manifest: Manifest = toml::from_str(
1374            r#"
1375    [package]
1376    name = "fixture"
1377    "#,
1378        )
1379        .unwrap();
1380        assert_eq!(default_manifest.orchestrator.drain.max_items, 1024);
1381        assert_eq!(default_manifest.orchestrator.drain.deadline_seconds, 30);
1382        assert_eq!(default_manifest.orchestrator.pumps.max_outstanding, 64);
1383
1384        let configured: Manifest = toml::from_str(
1385            r#"
1386    [package]
1387    name = "fixture"
1388
1389    [orchestrator]
1390    drain.max_items = 77
1391    drain.deadline_seconds = 12
1392    pumps.max_outstanding = 3
1393    "#,
1394        )
1395        .unwrap();
1396        assert_eq!(configured.orchestrator.drain.max_items, 77);
1397        assert_eq!(configured.orchestrator.drain.deadline_seconds, 12);
1398        assert_eq!(configured.orchestrator.pumps.max_outstanding, 3);
1399    }
1400
1401    #[test]
1402    fn load_check_config_stops_at_git_boundary() {
1403        let tmp = tempfile::tempdir().unwrap();
1404        // An ancestor harn.toml above .git must NOT be picked up.
1405        fs::write(
1406            tmp.path().join(MANIFEST),
1407            "[check]\npreflight_severity = \"off\"\n",
1408        )
1409        .unwrap();
1410        let project = tmp.path().join("project");
1411        std::fs::create_dir_all(project.join(".git")).unwrap();
1412        let inner = project.join("src");
1413        std::fs::create_dir_all(&inner).unwrap();
1414        let harn_file = inner.join("main.harn");
1415        fs::write(&harn_file, "pipeline main() {}\n").unwrap();
1416        let cfg = load_check_config(Some(&harn_file));
1417        assert!(
1418            cfg.preflight_severity.is_none(),
1419            "must not inherit harn.toml from outside the .git boundary"
1420        );
1421    }
1422}