Skip to main content

harn_cli/package/
manifest.rs

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