Skip to main content

harn_cli/package/
manifest.rs

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