Skip to main content

harn_cli/package/
manifest.rs

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