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