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, Default, 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    /// Registry index URL/path the dependency was originally added from.
707    /// Persisted in the manifest so registry provenance survives
708    /// round-trips and the lockfile can compare against the registry's
709    /// latest version.
710    #[serde(default)]
711    pub registry: Option<String>,
712    /// Registry-side package name (e.g. `@burin/notion-sdk`). May differ
713    /// from the alias and from the git URL's repo name.
714    #[serde(default, alias = "registry-name")]
715    pub registry_name: Option<String>,
716    /// Registry version specifier the dependency was added against.
717    #[serde(default, alias = "registry-version")]
718    pub registry_version: Option<String>,
719}
720
721impl Dependency {
722    pub(crate) fn git_url(&self) -> Option<&str> {
723        match self {
724            Dependency::Table(t) => t.git.as_deref(),
725            Dependency::Path(_) => None,
726        }
727    }
728
729    pub(crate) fn rev(&self) -> Option<&str> {
730        match self {
731            Dependency::Table(t) => t.rev.as_deref().or(t.tag.as_deref()),
732            Dependency::Path(_) => None,
733        }
734    }
735
736    pub(crate) fn branch(&self) -> Option<&str> {
737        match self {
738            Dependency::Table(t) => t.branch.as_deref(),
739            Dependency::Path(_) => None,
740        }
741    }
742
743    pub(crate) fn local_path(&self) -> Option<&str> {
744        match self {
745            Dependency::Table(t) => t.path.as_deref(),
746            Dependency::Path(p) => Some(p.as_str()),
747        }
748    }
749
750    pub(crate) fn registry_provenance(&self) -> Option<crate::package::RegistryProvenance> {
751        let Dependency::Table(table) = self else {
752            return None;
753        };
754        let source = table.registry.clone()?;
755        let name = table.registry_name.clone()?;
756        let version = table.registry_version.clone()?;
757        Some(crate::package::RegistryProvenance {
758            source,
759            name,
760            version,
761            provenance_url: None,
762        })
763    }
764}
765
766pub(crate) fn validate_package_alias(alias: &str) -> Result<(), PackageError> {
767    let valid = !alias.is_empty()
768        && alias != "."
769        && alias != ".."
770        && alias
771            .bytes()
772            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'));
773    if valid {
774        Ok(())
775    } else {
776        Err(PackageError::Validation(format!(
777            "invalid dependency alias {alias:?}; use ASCII letters, numbers, '.', '_' or '-'"
778        )))
779    }
780}
781
782pub(crate) fn toml_string_literal(value: &str) -> Result<String, PackageError> {
783    use std::fmt::Write as _;
784
785    let mut encoded = String::with_capacity(value.len() + 2);
786    encoded.push('"');
787    for ch in value.chars() {
788        match ch {
789            '\u{08}' => encoded.push_str("\\b"),
790            '\t' => encoded.push_str("\\t"),
791            '\n' => encoded.push_str("\\n"),
792            '\u{0C}' => encoded.push_str("\\f"),
793            '\r' => encoded.push_str("\\r"),
794            '"' => encoded.push_str("\\\""),
795            '\\' => encoded.push_str("\\\\"),
796            ch if ch <= '\u{1F}' || ch == '\u{7F}' => {
797                write!(&mut encoded, "\\u{:04X}", ch as u32).map_err(|error| {
798                    PackageError::Manifest(format!("failed to encode TOML string: {error}"))
799                })?;
800            }
801            ch => encoded.push(ch),
802        }
803    }
804    encoded.push('"');
805    Ok(encoded)
806}
807
808#[derive(Debug, Default, Clone)]
809pub struct RuntimeExtensions {
810    pub root_manifest: Option<Manifest>,
811    pub root_manifest_path: Option<PathBuf>,
812    pub root_manifest_dir: Option<PathBuf>,
813    pub llm: Option<harn_vm::llm_config::ProvidersConfig>,
814    pub capabilities: Option<harn_vm::llm::capabilities::CapabilitiesFile>,
815    pub hooks: Vec<ResolvedHookConfig>,
816    pub triggers: Vec<ResolvedTriggerConfig>,
817    pub handoff_routes: Vec<harn_vm::HandoffRouteConfig>,
818    pub provider_connectors: Vec<ResolvedProviderConnectorConfig>,
819}
820
821#[derive(Debug, Clone, Deserialize)]
822pub struct ProviderManifestEntry {
823    pub id: harn_vm::ProviderId,
824    pub connector: ProviderConnectorManifest,
825    #[serde(default)]
826    pub oauth: Option<ProviderOAuthManifest>,
827    #[serde(default)]
828    pub setup: Option<ProviderSetupManifest>,
829    #[serde(default)]
830    pub capabilities: ConnectorCapabilities,
831}
832
833#[derive(Debug, Clone, Deserialize)]
834pub struct ProviderConnectorManifest {
835    #[serde(default)]
836    pub harn: Option<String>,
837    #[serde(default)]
838    pub rust: Option<String>,
839}
840
841#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
842pub struct ProviderOAuthManifest {
843    #[serde(default, alias = "auth_url", alias = "authorization-endpoint")]
844    pub authorization_endpoint: Option<String>,
845    #[serde(default, alias = "token_url", alias = "token-endpoint")]
846    pub token_endpoint: Option<String>,
847    #[serde(default, alias = "registration_url", alias = "registration-endpoint")]
848    pub registration_endpoint: Option<String>,
849    #[serde(default)]
850    pub resource: Option<String>,
851    #[serde(default, alias = "scope")]
852    pub scopes: Option<String>,
853    #[serde(default, alias = "client-id")]
854    pub client_id: Option<String>,
855    #[serde(default, alias = "client-secret")]
856    pub client_secret: Option<String>,
857    #[serde(default, alias = "token_auth_method", alias = "token-auth-method")]
858    pub token_endpoint_auth_method: Option<String>,
859}
860
861#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
862pub struct ProviderSetupManifest {
863    #[serde(default, alias = "auth-type")]
864    pub auth_type: Option<String>,
865    #[serde(default)]
866    pub flow: Option<String>,
867    #[serde(default, alias = "required-scopes", alias = "scopes")]
868    pub required_scopes: Vec<String>,
869    #[serde(default, alias = "required-secrets")]
870    pub required_secrets: Vec<String>,
871    #[serde(default, alias = "setup-command")]
872    pub setup_command: Vec<String>,
873    #[serde(default, alias = "validation-command")]
874    pub validation_command: Vec<String>,
875    #[serde(default, alias = "health-checks")]
876    pub health_checks: Vec<ConnectorHealthCheckManifest>,
877    #[serde(default)]
878    pub recovery: ConnectorRecoveryCopy,
879    #[serde(flatten, default)]
880    pub extra: BTreeMap<String, toml::Value>,
881}
882
883#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
884pub struct ConnectorHealthCheckManifest {
885    pub id: String,
886    pub kind: String,
887    #[serde(default)]
888    pub command: Vec<String>,
889    #[serde(default)]
890    pub secret: Option<String>,
891    #[serde(default)]
892    pub url: Option<String>,
893}
894
895#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
896pub struct ConnectorRecoveryCopy {
897    #[serde(default, alias = "missing-install")]
898    pub missing_install: Option<String>,
899    #[serde(default, alias = "missing-auth")]
900    pub missing_auth: Option<String>,
901    #[serde(default, alias = "expired-credentials")]
902    pub expired_credentials: Option<String>,
903    #[serde(default, alias = "revoked-credentials")]
904    pub revoked_credentials: Option<String>,
905    #[serde(default, alias = "missing-scopes")]
906    pub missing_scopes: Option<String>,
907    #[serde(default, alias = "inaccessible-resource")]
908    pub inaccessible_resource: Option<String>,
909    #[serde(default, alias = "transient-provider-outage")]
910    pub transient_provider_outage: Option<String>,
911}
912
913#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
914pub struct ConnectorCapabilities {
915    pub webhook: bool,
916    pub oauth: bool,
917    pub rate_limit: bool,
918    pub pagination: bool,
919    pub graphql: bool,
920    pub streaming: bool,
921}
922
923impl ConnectorCapabilities {
924    pub const FEATURES: [&'static str; 6] = [
925        "webhook",
926        "oauth",
927        "rate_limit",
928        "pagination",
929        "graphql",
930        "streaming",
931    ];
932
933    fn enable(&mut self, feature: &str) -> Result<(), String> {
934        match normalize_connector_capability(feature).as_str() {
935            "webhook" => self.webhook = true,
936            "oauth" => self.oauth = true,
937            "rate_limit" => self.rate_limit = true,
938            "pagination" => self.pagination = true,
939            "graphql" => self.graphql = true,
940            "streaming" => self.streaming = true,
941            other => {
942                return Err(format!(
943                    "unknown connector capability '{feature}' (normalized as '{other}')"
944                ));
945            }
946        }
947        Ok(())
948    }
949}
950
951#[derive(Debug, Default, Deserialize)]
952struct ConnectorCapabilitiesTable {
953    #[serde(default)]
954    webhook: bool,
955    #[serde(default)]
956    oauth: bool,
957    #[serde(default, alias = "rate-limit")]
958    rate_limit: bool,
959    #[serde(default)]
960    pagination: bool,
961    #[serde(default)]
962    graphql: bool,
963    #[serde(default)]
964    streaming: bool,
965}
966
967impl From<ConnectorCapabilitiesTable> for ConnectorCapabilities {
968    fn from(value: ConnectorCapabilitiesTable) -> Self {
969        Self {
970            webhook: value.webhook,
971            oauth: value.oauth,
972            rate_limit: value.rate_limit,
973            pagination: value.pagination,
974            graphql: value.graphql,
975            streaming: value.streaming,
976        }
977    }
978}
979
980impl<'de> Deserialize<'de> for ConnectorCapabilities {
981    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
982    where
983        D: serde::Deserializer<'de>,
984    {
985        #[derive(Deserialize)]
986        #[serde(untagged)]
987        enum RawConnectorCapabilities {
988            List(Vec<String>),
989            Table(ConnectorCapabilitiesTable),
990        }
991
992        match RawConnectorCapabilities::deserialize(deserializer)? {
993            RawConnectorCapabilities::List(features) => {
994                let mut capabilities = ConnectorCapabilities::default();
995                for feature in features {
996                    capabilities
997                        .enable(&feature)
998                        .map_err(serde::de::Error::custom)?;
999                }
1000                Ok(capabilities)
1001            }
1002            RawConnectorCapabilities::Table(table) => Ok(table.into()),
1003        }
1004    }
1005}
1006
1007pub fn normalize_connector_capability(feature: &str) -> String {
1008    feature.trim().to_lowercase().replace('-', "_")
1009}
1010
1011#[derive(Debug, Clone, Default, Deserialize)]
1012pub struct ConnectorContractConfig {
1013    #[serde(default)]
1014    pub version: Option<u32>,
1015    #[serde(default)]
1016    pub fixtures: Vec<ConnectorContractFixture>,
1017}
1018
1019#[derive(Debug, Clone, Deserialize)]
1020pub struct ConnectorContractFixture {
1021    pub provider: harn_vm::ProviderId,
1022    #[serde(default)]
1023    pub name: Option<String>,
1024    #[serde(default)]
1025    pub kind: Option<String>,
1026    #[serde(default)]
1027    pub headers: BTreeMap<String, String>,
1028    #[serde(default)]
1029    pub query: BTreeMap<String, String>,
1030    #[serde(default)]
1031    pub metadata: Option<toml::Value>,
1032    #[serde(default)]
1033    pub body: Option<String>,
1034    #[serde(default)]
1035    pub body_json: Option<toml::Value>,
1036    #[serde(default)]
1037    pub expect_type: Option<String>,
1038    #[serde(default)]
1039    pub expect_kind: Option<String>,
1040    #[serde(default)]
1041    pub expect_dedupe_key: Option<String>,
1042    #[serde(default)]
1043    pub expect_signature_state: Option<String>,
1044    #[serde(default)]
1045    pub expect_payload_contains: Option<toml::Value>,
1046    #[serde(default)]
1047    pub expect_response_status: Option<u16>,
1048    #[serde(default)]
1049    pub expect_response_body: Option<toml::Value>,
1050    #[serde(default)]
1051    pub expect_event_count: Option<usize>,
1052    #[serde(default)]
1053    pub expect_error_contains: Option<String>,
1054}
1055
1056#[derive(Debug, Clone, PartialEq, Eq)]
1057pub enum ResolvedProviderConnectorKind {
1058    Harn { module: String },
1059    RustBuiltin,
1060    Invalid(String),
1061}
1062
1063#[derive(Debug, Clone)]
1064pub struct ResolvedProviderConnectorConfig {
1065    pub id: harn_vm::ProviderId,
1066    pub manifest_dir: PathBuf,
1067    pub connector: ResolvedProviderConnectorKind,
1068    pub oauth: Option<ProviderOAuthManifest>,
1069    pub setup: Option<ProviderSetupManifest>,
1070}
1071
1072#[derive(Debug, Clone)]
1073pub struct ResolvedHookConfig {
1074    pub event: harn_vm::orchestration::HookEvent,
1075    pub pattern: String,
1076    pub handler: String,
1077    pub manifest_dir: PathBuf,
1078    pub package_name: Option<String>,
1079    pub exports: HashMap<String, String>,
1080}
1081
1082#[derive(Debug, Clone)]
1083#[allow(dead_code)] // Trigger metadata is carried forward for harn#156 doctor and harn#159 dispatcher work.
1084pub struct ResolvedTriggerConfig {
1085    pub id: String,
1086    pub kind: TriggerKind,
1087    pub provider: harn_vm::ProviderId,
1088    pub autonomy_tier: harn_vm::AutonomyTier,
1089    pub match_: TriggerMatchExpr,
1090    pub when: Option<String>,
1091    pub when_budget: Option<TriggerWhenBudgetSpec>,
1092    pub handler: String,
1093    pub dedupe_key: Option<String>,
1094    pub retry: TriggerRetrySpec,
1095    pub dispatch_priority: TriggerDispatchPriority,
1096    pub budget: TriggerBudgetSpec,
1097    pub concurrency: Option<TriggerConcurrencyManifestSpec>,
1098    pub throttle: Option<TriggerThrottleManifestSpec>,
1099    pub rate_limit: Option<TriggerRateLimitManifestSpec>,
1100    pub debounce: Option<TriggerDebounceManifestSpec>,
1101    pub singleton: Option<TriggerSingletonManifestSpec>,
1102    pub batch: Option<TriggerBatchManifestSpec>,
1103    pub window: Option<TriggerStreamWindowManifestSpec>,
1104    pub priority_flow: Option<TriggerPriorityManifestSpec>,
1105    pub secrets: BTreeMap<String, String>,
1106    pub filter: Option<String>,
1107    pub kind_specific: BTreeMap<String, toml::Value>,
1108    pub manifest_dir: PathBuf,
1109    pub manifest_path: PathBuf,
1110    pub package_name: Option<String>,
1111    pub exports: HashMap<String, String>,
1112    pub table_index: usize,
1113    pub shape_error: Option<String>,
1114}
1115
1116#[derive(Debug, Clone)]
1117#[allow(dead_code)] // Collected bindings are validated now and consumed by harn#159 dispatcher work.
1118pub struct CollectedManifestTrigger {
1119    pub config: ResolvedTriggerConfig,
1120    pub handler: CollectedTriggerHandler,
1121    pub when: Option<CollectedTriggerPredicate>,
1122    pub flow_control: harn_vm::TriggerFlowControlConfig,
1123}
1124
1125#[derive(Debug, Clone)]
1126#[allow(dead_code)] // Remote targets and closures are retained for harn#159 trigger execution.
1127pub enum CollectedTriggerHandler {
1128    Local {
1129        reference: TriggerFunctionRef,
1130        closure: Rc<harn_vm::VmClosure>,
1131    },
1132    A2a {
1133        target: String,
1134        allow_cleartext: bool,
1135    },
1136    Worker {
1137        queue: String,
1138    },
1139    Persona {
1140        binding: harn_vm::PersonaRuntimeBinding,
1141    },
1142}
1143
1144#[derive(Debug, Clone)]
1145#[allow(dead_code)] // Predicate closures are validated now and reused by harn#161 dispatch gating.
1146pub struct CollectedTriggerPredicate {
1147    pub reference: TriggerFunctionRef,
1148    pub closure: Rc<harn_vm::VmClosure>,
1149}
1150
1151pub(crate) type ManifestModuleCacheKey = (PathBuf, Option<String>, Option<String>);
1152pub(crate) type ManifestModuleExports = BTreeMap<String, Rc<harn_vm::VmClosure>>;
1153
1154static MANIFEST_PROVIDER_SCHEMA_LOCK: OnceLock<tokio::sync::Mutex<()>> = OnceLock::new();
1155
1156pub(crate) async fn lock_manifest_provider_schemas() -> tokio::sync::MutexGuard<'static, ()> {
1157    MANIFEST_PROVIDER_SCHEMA_LOCK
1158        .get_or_init(|| tokio::sync::Mutex::new(()))
1159        .lock()
1160        .await
1161}
1162
1163pub(crate) fn read_manifest_from_path(path: &Path) -> Result<Manifest, PackageError> {
1164    let content = fs::read_to_string(path).map_err(|error| {
1165        if error.kind() == std::io::ErrorKind::NotFound {
1166            PackageError::Manifest(format!(
1167                "No {} found in {}.",
1168                MANIFEST,
1169                path.parent().unwrap_or_else(|| Path::new(".")).display()
1170            ))
1171        } else {
1172            PackageError::Manifest(format!("failed to read {}: {error}", path.display()))
1173        }
1174    })?;
1175    toml::from_str::<Manifest>(&content).map_err(|error| {
1176        PackageError::Manifest(format!("failed to parse {}: {error}", path.display()))
1177    })
1178}
1179
1180pub(crate) fn write_manifest_content(path: &Path, content: &str) -> Result<(), PackageError> {
1181    harn_vm::atomic_io::atomic_write(path, content.as_bytes()).map_err(|error| {
1182        PackageError::Manifest(format!("failed to write {}: {error}", path.display()))
1183    })
1184}
1185
1186pub(crate) fn absolutize_check_config_paths(
1187    mut config: CheckConfig,
1188    manifest_dir: &Path,
1189) -> CheckConfig {
1190    if let Some(path) = config.host_capabilities_path.clone() {
1191        let candidate = PathBuf::from(&path);
1192        if !candidate.is_absolute() {
1193            config.host_capabilities_path =
1194                Some(manifest_dir.join(candidate).display().to_string());
1195        }
1196    }
1197    if let Some(path) = config.bundle_root.clone() {
1198        let candidate = PathBuf::from(&path);
1199        if !candidate.is_absolute() {
1200            config.bundle_root = Some(manifest_dir.join(candidate).display().to_string());
1201        }
1202    }
1203    config
1204}
1205
1206/// Walk upward from `start` (or its parent if it's a file path that
1207/// does not yet exist) looking for the nearest `harn.toml`. Stops at
1208/// a `.git` boundary so a stray manifest in `$HOME` or a parent
1209/// project is never silently picked up. Returns `(manifest, manifest_dir)`
1210/// when found.
1211pub(crate) fn find_nearest_manifest(start: &Path) -> Option<(Manifest, PathBuf)> {
1212    const MAX_PARENT_DIRS: usize = 16;
1213    let base = if start.is_absolute() {
1214        start.to_path_buf()
1215    } else {
1216        std::env::current_dir()
1217            .unwrap_or_else(|_| PathBuf::from("."))
1218            .join(start)
1219    };
1220    let mut cursor: Option<PathBuf> = if base.is_dir() {
1221        Some(base)
1222    } else {
1223        base.parent().map(Path::to_path_buf)
1224    };
1225    let mut steps = 0usize;
1226    while let Some(dir) = cursor {
1227        if steps >= MAX_PARENT_DIRS {
1228            break;
1229        }
1230        steps += 1;
1231        let candidate = dir.join(MANIFEST);
1232        if candidate.is_file() {
1233            match read_manifest_from_path(&candidate) {
1234                Ok(manifest) => return Some((manifest, dir)),
1235                Err(error) => {
1236                    eprintln!("warning: {error}");
1237                    return None;
1238                }
1239            }
1240        }
1241        if dir.join(".git").exists() {
1242            break;
1243        }
1244        cursor = dir.parent().map(Path::to_path_buf);
1245    }
1246    None
1247}
1248
1249/// Load the `[check]` config from the nearest `harn.toml`.
1250/// Walks up from the given file (or from cwd if no file is given),
1251/// stopping at a `.git` boundary.
1252pub fn load_check_config(harn_file: Option<&std::path::Path>) -> CheckConfig {
1253    let anchor = harn_file
1254        .map(Path::to_path_buf)
1255        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1256    if let Some((manifest, dir)) = find_nearest_manifest(&anchor) {
1257        return absolutize_check_config_paths(manifest.check, &dir);
1258    }
1259    CheckConfig::default()
1260}
1261
1262/// Load the `[workspace]` config and the directory of the `harn.toml`
1263/// it came from. Paths in the returned config are left as-is (callers
1264/// resolve them against the returned `manifest_dir`).
1265pub fn load_workspace_config(anchor: Option<&Path>) -> Option<(WorkspaceConfig, PathBuf)> {
1266    let anchor = anchor
1267        .map(Path::to_path_buf)
1268        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1269    let (manifest, dir) = find_nearest_manifest(&anchor)?;
1270    Some((manifest.workspace, dir))
1271}
1272
1273pub fn load_package_eval_pack_paths(anchor: Option<&Path>) -> Result<Vec<PathBuf>, PackageError> {
1274    let anchor = anchor
1275        .map(Path::to_path_buf)
1276        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
1277    let Some((manifest, dir)) = find_nearest_manifest(&anchor) else {
1278        return Err(PackageError::Manifest(
1279            "no harn.toml found for package eval discovery".to_string(),
1280        ));
1281    };
1282
1283    let declared = manifest
1284        .package
1285        .as_ref()
1286        .map(|package| package.evals.clone())
1287        .unwrap_or_default();
1288    let mut paths = if declared.is_empty() {
1289        let default_pack = dir.join("harn.eval.toml");
1290        if default_pack.is_file() {
1291            vec![default_pack]
1292        } else {
1293            Vec::new()
1294        }
1295    } else {
1296        declared
1297            .iter()
1298            .map(|entry| {
1299                let path = PathBuf::from(entry);
1300                if path.is_absolute() {
1301                    path
1302                } else {
1303                    dir.join(path)
1304                }
1305            })
1306            .collect()
1307    };
1308    paths.sort();
1309    if paths.is_empty() {
1310        return Err(PackageError::Manifest(
1311            "package declares no eval packs; add [package].evals or harn.eval.toml".to_string(),
1312        ));
1313    }
1314    for path in &paths {
1315        if !path.is_file() {
1316            return Err(PackageError::Manifest(format!(
1317                "eval pack does not exist: {}",
1318                path.display()
1319            )));
1320        }
1321    }
1322    Ok(paths)
1323}
1324
1325#[derive(Debug, Clone)]
1326pub(crate) struct ManifestContext {
1327    pub(crate) manifest: Manifest,
1328    pub(crate) dir: PathBuf,
1329}
1330
1331impl ManifestContext {
1332    pub(crate) fn manifest_path(&self) -> PathBuf {
1333        self.dir.join(MANIFEST)
1334    }
1335
1336    pub(crate) fn lock_path(&self) -> PathBuf {
1337        self.dir.join(LOCK_FILE)
1338    }
1339
1340    pub(crate) fn packages_dir(&self) -> PathBuf {
1341        self.dir.join(PKG_DIR)
1342    }
1343}
1344
1345#[derive(Debug, Clone)]
1346pub(crate) struct PackageWorkspace {
1347    manifest_dir: PathBuf,
1348    cache_dir: Option<PathBuf>,
1349    registry_source: Option<String>,
1350    read_process_env: bool,
1351}
1352
1353impl PackageWorkspace {
1354    pub(crate) fn from_current_dir() -> Result<Self, PackageError> {
1355        let manifest_dir = std::env::current_dir()
1356            .map_err(|error| PackageError::Manifest(format!("failed to read cwd: {error}")))?;
1357        Ok(Self {
1358            manifest_dir,
1359            cache_dir: None,
1360            registry_source: None,
1361            read_process_env: true,
1362        })
1363    }
1364
1365    #[cfg(test)]
1366    pub(crate) fn for_test(
1367        manifest_dir: impl Into<PathBuf>,
1368        cache_dir: impl Into<PathBuf>,
1369    ) -> Self {
1370        Self {
1371            manifest_dir: manifest_dir.into(),
1372            cache_dir: Some(cache_dir.into()),
1373            registry_source: None,
1374            read_process_env: false,
1375        }
1376    }
1377
1378    #[cfg(test)]
1379    pub(crate) fn with_registry_source(mut self, source: impl Into<String>) -> Self {
1380        self.registry_source = Some(source.into());
1381        self
1382    }
1383
1384    pub(crate) fn manifest_dir(&self) -> &Path {
1385        &self.manifest_dir
1386    }
1387
1388    pub(crate) fn load_manifest_context(&self) -> Result<ManifestContext, PackageError> {
1389        let manifest_path = self.manifest_dir.join(MANIFEST);
1390        let manifest = read_manifest_from_path(&manifest_path)?;
1391        Ok(ManifestContext {
1392            manifest,
1393            dir: self.manifest_dir.clone(),
1394        })
1395    }
1396
1397    pub(crate) fn cache_root(&self) -> Result<PathBuf, PackageError> {
1398        if let Some(cache_dir) = &self.cache_dir {
1399            return Ok(cache_dir.clone());
1400        }
1401        if self.read_process_env {
1402            if let Ok(value) = std::env::var(HARN_CACHE_DIR_ENV) {
1403                if !value.trim().is_empty() {
1404                    return Ok(PathBuf::from(value));
1405                }
1406            }
1407        }
1408
1409        let home = std::env::var_os("HOME")
1410            .map(PathBuf::from)
1411            .ok_or_else(|| "HOME is not set and HARN_CACHE_DIR was not provided".to_string())?;
1412        if cfg!(target_os = "macos") {
1413            return Ok(home.join("Library/Caches/harn"));
1414        }
1415        if let Some(xdg) = std::env::var_os("XDG_CACHE_HOME") {
1416            return Ok(PathBuf::from(xdg).join("harn"));
1417        }
1418        Ok(home.join(".cache/harn"))
1419    }
1420
1421    pub(crate) fn resolve_registry_source(
1422        &self,
1423        explicit: Option<&str>,
1424    ) -> Result<String, PackageError> {
1425        if let Some(explicit) = explicit.map(str::trim).filter(|value| !value.is_empty()) {
1426            return Ok(explicit.to_string());
1427        }
1428        if let Some(source) = self
1429            .registry_source
1430            .as_deref()
1431            .map(str::trim)
1432            .filter(|value| !value.is_empty())
1433        {
1434            if Url::parse(source).is_ok() || PathBuf::from(source).is_absolute() {
1435                return Ok(source.to_string());
1436            }
1437            return Ok(self.manifest_dir.join(source).display().to_string());
1438        }
1439        if self.read_process_env {
1440            if let Ok(value) = std::env::var(HARN_PACKAGE_REGISTRY_ENV) {
1441                let value = value.trim();
1442                if !value.is_empty() {
1443                    return Ok(value.to_string());
1444                }
1445            }
1446        }
1447
1448        if let Some((manifest, manifest_dir)) = find_nearest_manifest(&self.manifest_dir) {
1449            if let Some(raw) = manifest
1450                .registry
1451                .url
1452                .as_deref()
1453                .map(str::trim)
1454                .filter(|value| !value.is_empty())
1455            {
1456                if Url::parse(raw).is_ok() || PathBuf::from(raw).is_absolute() {
1457                    return Ok(raw.to_string());
1458                }
1459                return Ok(manifest_dir.join(raw).display().to_string());
1460            }
1461        }
1462
1463        Ok(DEFAULT_PACKAGE_REGISTRY_URL.to_string())
1464    }
1465}
1466
1467#[cfg(test)]
1468mod tests {
1469    use super::*;
1470
1471    #[test]
1472    fn package_eval_pack_paths_use_package_manifest_entries() {
1473        let tmp = tempfile::tempdir().unwrap();
1474        let root = tmp.path();
1475        fs::create_dir_all(root.join(".git")).unwrap();
1476        fs::create_dir_all(root.join("evals")).unwrap();
1477        fs::write(
1478            root.join(MANIFEST),
1479            r#"
1480    [package]
1481    name = "demo"
1482    version = "0.1.0"
1483    evals = ["evals/webhook.toml"]
1484    "#,
1485        )
1486        .unwrap();
1487        fs::write(
1488            root.join("evals/webhook.toml"),
1489            "version = 1\n[[cases]]\nrun = \"run.json\"\n",
1490        )
1491        .unwrap();
1492
1493        let paths = load_package_eval_pack_paths(Some(&root.join("src/main.harn"))).unwrap();
1494
1495        assert_eq!(paths, vec![root.join("evals/webhook.toml")]);
1496    }
1497    #[test]
1498    fn preflight_severity_parsing_accepts_synonyms() {
1499        assert_eq!(
1500            PreflightSeverity::from_opt(Some("warning")),
1501            PreflightSeverity::Warning
1502        );
1503        assert_eq!(
1504            PreflightSeverity::from_opt(Some("WARN")),
1505            PreflightSeverity::Warning
1506        );
1507        assert_eq!(
1508            PreflightSeverity::from_opt(Some("off")),
1509            PreflightSeverity::Off
1510        );
1511        assert_eq!(
1512            PreflightSeverity::from_opt(Some("allow")),
1513            PreflightSeverity::Off
1514        );
1515        assert_eq!(
1516            PreflightSeverity::from_opt(Some("error")),
1517            PreflightSeverity::Error
1518        );
1519        assert_eq!(PreflightSeverity::from_opt(None), PreflightSeverity::Error);
1520        // Unknown values fall back to the safe default (error).
1521        assert_eq!(
1522            PreflightSeverity::from_opt(Some("bogus")),
1523            PreflightSeverity::Error
1524        );
1525    }
1526
1527    #[test]
1528    fn load_check_config_walks_up_from_nested_file() {
1529        let tmp = tempfile::tempdir().unwrap();
1530        let root = tmp.path();
1531        // Mark root as project boundary so walk-up terminates here.
1532        std::fs::create_dir_all(root.join(".git")).unwrap();
1533        fs::write(
1534            root.join(MANIFEST),
1535            r#"
1536    [check]
1537    preflight_severity = "warning"
1538    preflight_allow = ["custom.scan", "runtime.*"]
1539    host_capabilities_path = "./schemas/host-caps.json"
1540
1541    [workspace]
1542    pipelines = ["pipelines", "scripts"]
1543    "#,
1544        )
1545        .unwrap();
1546        let nested = root.join("src").join("deep");
1547        std::fs::create_dir_all(&nested).unwrap();
1548        let harn_file = nested.join("pipeline.harn");
1549        fs::write(&harn_file, "pipeline main() {}\n").unwrap();
1550
1551        let cfg = load_check_config(Some(&harn_file));
1552        assert_eq!(cfg.preflight_severity.as_deref(), Some("warning"));
1553        assert_eq!(cfg.preflight_allow, vec!["custom.scan", "runtime.*"]);
1554        let caps_path = cfg.host_capabilities_path.expect("host caps path");
1555        assert!(
1556            caps_path.ends_with("schemas/host-caps.json")
1557                || caps_path.ends_with("schemas\\host-caps.json"),
1558            "unexpected absolutized path: {caps_path}"
1559        );
1560
1561        let (workspace, manifest_dir) =
1562            load_workspace_config(Some(&harn_file)).expect("workspace manifest");
1563        assert_eq!(workspace.pipelines, vec!["pipelines", "scripts"]);
1564        // Walk-up lands on the directory containing the harn.toml.
1565        assert_eq!(manifest_dir, root);
1566    }
1567
1568    #[test]
1569    fn orchestrator_drain_config_parses_defaults_and_overrides() {
1570        let default_manifest: Manifest = toml::from_str(
1571            r#"
1572    [package]
1573    name = "fixture"
1574    "#,
1575        )
1576        .unwrap();
1577        assert_eq!(default_manifest.orchestrator.drain.max_items, 1024);
1578        assert_eq!(default_manifest.orchestrator.drain.deadline_seconds, 30);
1579        assert_eq!(default_manifest.orchestrator.pumps.max_outstanding, 64);
1580
1581        let configured: Manifest = toml::from_str(
1582            r#"
1583    [package]
1584    name = "fixture"
1585
1586    [orchestrator]
1587    drain.max_items = 77
1588    drain.deadline_seconds = 12
1589    pumps.max_outstanding = 3
1590    "#,
1591        )
1592        .unwrap();
1593        assert_eq!(configured.orchestrator.drain.max_items, 77);
1594        assert_eq!(configured.orchestrator.drain.deadline_seconds, 12);
1595        assert_eq!(configured.orchestrator.pumps.max_outstanding, 3);
1596    }
1597
1598    #[test]
1599    fn load_check_config_stops_at_git_boundary() {
1600        let tmp = tempfile::tempdir().unwrap();
1601        // An ancestor harn.toml above .git must NOT be picked up.
1602        fs::write(
1603            tmp.path().join(MANIFEST),
1604            "[check]\npreflight_severity = \"off\"\n",
1605        )
1606        .unwrap();
1607        let project = tmp.path().join("project");
1608        std::fs::create_dir_all(project.join(".git")).unwrap();
1609        let inner = project.join("src");
1610        std::fs::create_dir_all(&inner).unwrap();
1611        let harn_file = inner.join("main.harn");
1612        fs::write(&harn_file, "pipeline main() {}\n").unwrap();
1613        let cfg = load_check_config(Some(&harn_file));
1614        assert!(
1615            cfg.preflight_severity.is_none(),
1616            "must not inherit harn.toml from outside the .git boundary"
1617        );
1618    }
1619}