Skip to main content

harn_stdlib/
lib.rs

1//! Canonical embedded Harn standard library source catalog.
2//!
3//! This crate intentionally contains only static source strings so runtime and
4//! static tooling crates can share the same stdlib modules without depending on
5//! each other.
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct StdlibSource {
9    pub module: &'static str,
10    pub source: &'static str,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct StdlibPromptAsset {
15    pub path: &'static str,
16    pub source: &'static str,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct StdlibPublicFunction {
21    pub name: String,
22    pub signature: String,
23    pub required_params: usize,
24    pub total_params: usize,
25    pub variadic: bool,
26    pub doc: Option<String>,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct StdlibEntrypointModule {
31    pub import_path: String,
32    pub category: String,
33}
34
35pub const STDLIB_SOURCES: &[StdlibSource] = &[
36    StdlibSource {
37        module: "text",
38        source: include_str!("stdlib/stdlib_text.harn"),
39    },
40    StdlibSource {
41        module: "ansi",
42        source: include_str!("stdlib/stdlib_ansi.harn"),
43    },
44    StdlibSource {
45        module: "table",
46        source: include_str!("stdlib/stdlib_table.harn"),
47    },
48    StdlibSource {
49        module: "diff",
50        source: include_str!("stdlib/stdlib_diff.harn"),
51    },
52    StdlibSource {
53        module: "edit",
54        source: include_str!("stdlib/stdlib_edit.harn"),
55    },
56    StdlibSource {
57        module: "artifact/web",
58        source: include_str!("stdlib/artifact/web.harn"),
59    },
60    StdlibSource {
61        module: "collections",
62        source: include_str!("stdlib/stdlib_collections.harn"),
63    },
64    StdlibSource {
65        module: "math",
66        source: include_str!("stdlib/stdlib_math.harn"),
67    },
68    StdlibSource {
69        module: "path",
70        source: include_str!("stdlib/stdlib_path.harn"),
71    },
72    StdlibSource {
73        module: "fs",
74        source: include_str!("stdlib/stdlib_fs.harn"),
75    },
76    StdlibSource {
77        module: "run_artifacts",
78        source: include_str!("stdlib/stdlib_run_artifacts.harn"),
79    },
80    StdlibSource {
81        module: "os",
82        source: include_str!("stdlib/stdlib_os.harn"),
83    },
84    StdlibSource {
85        module: "json",
86        source: include_str!("stdlib/stdlib_json.harn"),
87    },
88    StdlibSource {
89        module: "json/stream",
90        source: include_str!("stdlib/stdlib_json_stream.harn"),
91    },
92    StdlibSource {
93        module: "cache",
94        source: include_str!("stdlib/stdlib_cache.harn"),
95    },
96    StdlibSource {
97        module: "observability",
98        source: include_str!("stdlib/stdlib_observability.harn"),
99    },
100    StdlibSource {
101        module: "tools",
102        source: include_str!("stdlib/stdlib_tools.harn"),
103    },
104    StdlibSource {
105        module: "composition",
106        source: include_str!("stdlib/stdlib_composition.harn"),
107    },
108    StdlibSource {
109        module: "web",
110        source: include_str!("stdlib/stdlib_web.harn"),
111    },
112    StdlibSource {
113        module: "graphql",
114        source: include_str!("stdlib/stdlib_graphql.harn"),
115    },
116    StdlibSource {
117        module: "schema",
118        source: include_str!("stdlib/stdlib_schema.harn"),
119    },
120    StdlibSource {
121        module: "testing",
122        source: include_str!("stdlib/stdlib_testing.harn"),
123    },
124    StdlibSource {
125        module: "files",
126        source: include_str!("stdlib/stdlib_files.harn"),
127    },
128    StdlibSource {
129        module: "mcp",
130        source: include_str!("stdlib/stdlib_mcp.harn"),
131    },
132    StdlibSource {
133        module: "vision",
134        source: include_str!("stdlib/stdlib_vision.harn"),
135    },
136    StdlibSource {
137        module: "context",
138        source: include_str!("stdlib/stdlib_context.harn"),
139    },
140    StdlibSource {
141        module: "context/maintenance",
142        source: include_str!("stdlib/context/maintenance.harn"),
143    },
144    StdlibSource {
145        module: "runtime",
146        source: include_str!("stdlib/stdlib_runtime.harn"),
147    },
148    StdlibSource {
149        module: "io",
150        source: include_str!("stdlib/stdlib_io.harn"),
151    },
152    StdlibSource {
153        module: "command",
154        source: include_str!("stdlib/stdlib_command.harn"),
155    },
156    StdlibSource {
157        module: "signal",
158        source: include_str!("stdlib/stdlib_signal.harn"),
159    },
160    StdlibSource {
161        module: "net_policy",
162        source: include_str!("stdlib/stdlib_net_policy.harn"),
163    },
164    StdlibSource {
165        module: "review",
166        source: include_str!("stdlib/stdlib_review.harn"),
167    },
168    StdlibSource {
169        module: "experiments",
170        source: include_str!("stdlib/stdlib_experiments.harn"),
171    },
172    StdlibSource {
173        module: "project",
174        source: include_str!("stdlib/stdlib_project.harn"),
175    },
176    StdlibSource {
177        module: "prompt_library",
178        source: include_str!("stdlib/stdlib_prompt_library.harn"),
179    },
180    StdlibSource {
181        module: "async",
182        source: include_str!("stdlib/stdlib_async.harn"),
183    },
184    StdlibSource {
185        module: "poll",
186        source: include_str!("stdlib/stdlib_poll.harn"),
187    },
188    StdlibSource {
189        module: "coerce",
190        source: include_str!("stdlib/stdlib_coerce.harn"),
191    },
192    StdlibSource {
193        module: "settled",
194        source: include_str!("stdlib/stdlib_settled.harn"),
195    },
196    StdlibSource {
197        module: "cli",
198        source: include_str!("stdlib/stdlib_cli.harn"),
199    },
200    StdlibSource {
201        module: "gha",
202        source: include_str!("stdlib/stdlib_gha.harn"),
203    },
204    StdlibSource {
205        module: "tui",
206        source: include_str!("stdlib/stdlib_tui.harn"),
207    },
208    StdlibSource {
209        module: "jsonl",
210        source: include_str!("stdlib/stdlib_jsonl.harn"),
211    },
212    StdlibSource {
213        module: "config",
214        source: include_str!("stdlib/stdlib_config.harn"),
215    },
216    StdlibSource {
217        module: "calendar",
218        source: include_str!("stdlib/stdlib_calendar.harn"),
219    },
220    StdlibSource {
221        module: "agents",
222        source: include_str!("stdlib/stdlib_agents.harn"),
223    },
224    StdlibSource {
225        module: "lifecycle/pool",
226        source: include_str!("stdlib/lifecycle/pool.harn"),
227    },
228    StdlibSource {
229        module: "lifecycle/combinators",
230        source: include_str!("stdlib/lifecycle/combinators.harn"),
231    },
232    StdlibSource {
233        module: "lifecycle/on_budget",
234        source: include_str!("stdlib/lifecycle/on_budget.harn"),
235    },
236    StdlibSource {
237        module: "agent/prompts",
238        source: include_str!("stdlib/agent/prompts.harn"),
239    },
240    StdlibSource {
241        module: "llm/media",
242        source: include_str!("stdlib/llm/media.harn"),
243    },
244    StdlibSource {
245        module: "llm/options",
246        source: include_str!("stdlib/llm/options.harn"),
247    },
248    StdlibSource {
249        module: "llm/catalog",
250        source: include_str!("stdlib/llm/catalog.harn"),
251    },
252    StdlibSource {
253        module: "llm/safe",
254        source: include_str!("stdlib/llm/safe.harn"),
255    },
256    StdlibSource {
257        module: "llm/budget",
258        source: include_str!("stdlib/llm/budget.harn"),
259    },
260    StdlibSource {
261        module: "llm/economics",
262        source: include_str!("stdlib/llm/economics.harn"),
263    },
264    StdlibSource {
265        module: "llm/prompts",
266        source: include_str!("stdlib/llm/prompts.harn"),
267    },
268    StdlibSource {
269        module: "llm/defaults",
270        source: include_str!("stdlib/llm/defaults.harn"),
271    },
272    StdlibSource {
273        module: "llm/handlers",
274        source: include_str!("stdlib/llm/handlers.harn"),
275    },
276    StdlibSource {
277        module: "llm/tool_telemetry",
278        source: include_str!("stdlib/llm/tool_telemetry.harn"),
279    },
280    StdlibSource {
281        module: "llm/tool_middleware",
282        source: include_str!("stdlib/llm/tool_middleware.harn"),
283    },
284    StdlibSource {
285        module: "llm/tool_binder",
286        source: include_str!("stdlib/llm/tool_binder.harn"),
287    },
288    StdlibSource {
289        module: "llm/refine",
290        source: include_str!("stdlib/llm/refine.harn"),
291    },
292    StdlibSource {
293        module: "llm/ensemble",
294        source: include_str!("stdlib/llm/ensemble.harn"),
295    },
296    StdlibSource {
297        module: "llm/rerank",
298        source: include_str!("stdlib/llm/rerank.harn"),
299    },
300    StdlibSource {
301        module: "agent/reasoning",
302        source: include_str!("stdlib/agent/reasoning.harn"),
303    },
304    StdlibSource {
305        module: "agent/options",
306        source: include_str!("stdlib/agent/options.harn"),
307    },
308    StdlibSource {
309        module: "llm/judge",
310        source: include_str!("stdlib/llm/judge.harn"),
311    },
312    StdlibSource {
313        module: "llm/optimize",
314        source: include_str!("stdlib/llm/optimize.harn"),
315    },
316    StdlibSource {
317        module: "agent/events",
318        source: include_str!("stdlib/agent/events.harn"),
319    },
320    StdlibSource {
321        module: "agent/primitives",
322        source: include_str!("stdlib/agent/primitives.harn"),
323    },
324    StdlibSource {
325        module: "agent/progress",
326        source: include_str!("stdlib/agent/progress.harn"),
327    },
328    StdlibSource {
329        module: "agent/loop",
330        source: include_str!("stdlib/agent/loop.harn"),
331    },
332    StdlibSource {
333        module: "agent/chat",
334        source: include_str!("stdlib/agent/chat.harn"),
335    },
336    StdlibSource {
337        module: "agent/user",
338        source: include_str!("stdlib/agent/user.harn"),
339    },
340    StdlibSource {
341        module: "agent/tool_search",
342        source: include_str!("stdlib/agent/tool_search.harn"),
343    },
344    StdlibSource {
345        module: "agent/turn",
346        source: include_str!("stdlib/agent/turn.harn"),
347    },
348    StdlibSource {
349        module: "agent/workers",
350        source: include_str!("stdlib/agent/workers.harn"),
351    },
352    StdlibSource {
353        module: "agent/resume_by",
354        source: include_str!("stdlib/agent/resume_by.harn"),
355    },
356    StdlibSource {
357        module: "agent/state",
358        source: include_str!("stdlib/agent/state.harn"),
359    },
360    StdlibSource {
361        module: "agent/skills",
362        source: include_str!("stdlib/agent/skills.harn"),
363    },
364    StdlibSource {
365        module: "agent/autocompact",
366        source: include_str!("stdlib/agent/autocompact.harn"),
367    },
368    StdlibSource {
369        module: "agent/mcp",
370        source: include_str!("stdlib/agent/mcp.harn"),
371    },
372    StdlibSource {
373        module: "agent/host_tools",
374        source: include_str!("stdlib/agent/host_tools.harn"),
375    },
376    StdlibSource {
377        module: "agent/budget",
378        source: include_str!("stdlib/agent/budget.harn"),
379    },
380    StdlibSource {
381        module: "agent/daemon",
382        source: include_str!("stdlib/agent/daemon.harn"),
383    },
384    StdlibSource {
385        module: "agent/preflight",
386        source: include_str!("stdlib/agent/preflight.harn"),
387    },
388    StdlibSource {
389        module: "agent/postturn",
390        source: include_str!("stdlib/agent/postturn.harn"),
391    },
392    StdlibSource {
393        module: "agent/judge",
394        source: include_str!("stdlib/agent/judge.harn"),
395    },
396    StdlibSource {
397        module: "agent/presets",
398        source: include_str!("stdlib/agent/presets.harn"),
399    },
400    StdlibSource {
401        module: "agent_state",
402        source: include_str!("stdlib/stdlib_agent_state.harn"),
403    },
404    StdlibSource {
405        module: "memory",
406        source: include_str!("stdlib/stdlib_memory.harn"),
407    },
408    StdlibSource {
409        module: "postgres",
410        source: include_str!("stdlib/stdlib_postgres.harn"),
411    },
412    StdlibSource {
413        module: "checkpoint",
414        source: include_str!("stdlib/stdlib_checkpoint.harn"),
415    },
416    StdlibSource {
417        module: "host",
418        source: include_str!("stdlib/stdlib_host.harn"),
419    },
420    StdlibSource {
421        module: "git",
422        source: include_str!("stdlib/stdlib_git.harn"),
423    },
424    StdlibSource {
425        module: "hitl",
426        source: include_str!("stdlib/stdlib_hitl.harn"),
427    },
428    StdlibSource {
429        module: "trust",
430        source: include_str!("stdlib/stdlib_trust.harn"),
431    },
432    StdlibSource {
433        module: "corrections",
434        source: include_str!("stdlib/stdlib_corrections.harn"),
435    },
436    StdlibSource {
437        module: "plan",
438        source: include_str!("stdlib/stdlib_plan.harn"),
439    },
440    StdlibSource {
441        module: "waitpoints",
442        source: include_str!("stdlib/stdlib_waitpoints.harn"),
443    },
444    StdlibSource {
445        module: "waitpoint",
446        source: include_str!("stdlib/stdlib_waitpoint.harn"),
447    },
448    StdlibSource {
449        module: "monitors",
450        source: include_str!("stdlib/stdlib_monitors.harn"),
451    },
452    StdlibSource {
453        module: "worktree",
454        source: include_str!("stdlib/stdlib_worktree.harn"),
455    },
456    StdlibSource {
457        module: "acp",
458        source: include_str!("stdlib/stdlib_acp.harn"),
459    },
460    StdlibSource {
461        module: "triggers",
462        source: include_str!("stdlib/stdlib_triggers.harn"),
463    },
464    StdlibSource {
465        module: "triage",
466        source: include_str!("stdlib/stdlib_triage.harn"),
467    },
468    StdlibSource {
469        module: "dashboard/jobs",
470        source: include_str!("stdlib/dashboard/jobs.harn"),
471    },
472    StdlibSource {
473        module: "ui_resource",
474        source: include_str!("stdlib/stdlib_ui_resource.harn"),
475    },
476    StdlibSource {
477        module: "handoffs",
478        source: include_str!("stdlib/stdlib_handoffs.harn"),
479    },
480    StdlibSource {
481        module: "lifecycle",
482        source: include_str!("stdlib/stdlib_lifecycle.harn"),
483    },
484    StdlibSource {
485        module: "tool_hooks_catalogues",
486        source: include_str!("stdlib/stdlib_tool_hooks_catalogues.harn"),
487    },
488    StdlibSource {
489        module: "tool_hooks",
490        source: include_str!("stdlib/stdlib_tool_hooks.harn"),
491    },
492    StdlibSource {
493        module: "channel_guardrails",
494        source: include_str!("stdlib/stdlib_channel_guardrails.harn"),
495    },
496    StdlibSource {
497        module: "personas/prelude",
498        source: include_str!("stdlib/stdlib_personas_prelude.harn"),
499    },
500    StdlibSource {
501        module: "personas/bulletins",
502        source: include_str!("stdlib/stdlib_personas_bulletins.harn"),
503    },
504    StdlibSource {
505        module: "connectors/shared",
506        source: include_str!("stdlib/stdlib_connectors_shared.harn"),
507    },
508    StdlibSource {
509        module: "oauth/providers",
510        source: include_str!("stdlib/oauth/providers.harn"),
511    },
512    StdlibSource {
513        module: "oauth/storage",
514        source: include_str!("stdlib/oauth/storage.harn"),
515    },
516    StdlibSource {
517        module: "oauth/client",
518        source: include_str!("stdlib/oauth/client.harn"),
519    },
520    StdlibSource {
521        module: "oauth/device_flow",
522        source: include_str!("stdlib/oauth/device_flow.harn"),
523    },
524    StdlibSource {
525        module: "oauth/redaction",
526        source: include_str!("stdlib/oauth/redaction.harn"),
527    },
528    StdlibSource {
529        module: "oauth/dynamic_registration",
530        source: include_str!("stdlib/oauth/dynamic_registration.harn"),
531    },
532    StdlibSource {
533        module: "connectors/github",
534        source: include_str!("stdlib/stdlib_connectors_github.harn"),
535    },
536    StdlibSource {
537        module: "connectors/linear",
538        source: include_str!("stdlib/stdlib_connectors_linear.harn"),
539    },
540    StdlibSource {
541        module: "connectors/notion",
542        source: include_str!("stdlib/stdlib_connectors_notion.harn"),
543    },
544    StdlibSource {
545        module: "connectors/slack",
546        source: include_str!("stdlib/stdlib_connectors_slack.harn"),
547    },
548    StdlibSource {
549        module: "workflow/prompts",
550        source: include_str!("stdlib/workflow/prompts.harn"),
551    },
552    StdlibSource {
553        module: "workflow/context",
554        source: include_str!("stdlib/workflow/context.harn"),
555    },
556    StdlibSource {
557        module: "workflow/options",
558        source: include_str!("stdlib/workflow/options.harn"),
559    },
560    StdlibSource {
561        module: "workflow/checkpoints",
562        source: include_str!("stdlib/workflow/checkpoints.harn"),
563    },
564    StdlibSource {
565        module: "workflow/stage",
566        source: include_str!("stdlib/workflow/stage.harn"),
567    },
568    StdlibSource {
569        module: "workflow/map",
570        source: include_str!("stdlib/workflow/map.harn"),
571    },
572    StdlibSource {
573        module: "workflow/schedule",
574        source: include_str!("stdlib/workflow/schedule.harn"),
575    },
576    StdlibSource {
577        module: "workflow/execute",
578        source: include_str!("stdlib/workflow/execute.harn"),
579    },
580];
581
582pub const STDLIB_PROMPT_ASSETS: &[StdlibPromptAsset] = &[
583    StdlibPromptAsset {
584        path: "agent/prompts/tool_contract_text.harn.prompt",
585        source: include_str!("stdlib/agent/prompts/tool_contract_text.harn.prompt"),
586    },
587    StdlibPromptAsset {
588        path: "agent/prompts/tool_contract_native.harn.prompt",
589        source: include_str!("stdlib/agent/prompts/tool_contract_native.harn.prompt"),
590    },
591    StdlibPromptAsset {
592        path: "agent/prompts/tool_contract_text_response_protocol.harn.prompt",
593        source: include_str!(
594            "stdlib/agent/prompts/tool_contract_text_response_protocol.harn.prompt"
595        ),
596    },
597    StdlibPromptAsset {
598        path: "agent/prompts/tool_contract_action_native.harn.prompt",
599        source: include_str!("stdlib/agent/prompts/tool_contract_action_native.harn.prompt"),
600    },
601    StdlibPromptAsset {
602        path: "agent/prompts/tool_contract_action_text.harn.prompt",
603        source: include_str!("stdlib/agent/prompts/tool_contract_action_text.harn.prompt"),
604    },
605    StdlibPromptAsset {
606        path: "agent/prompts/tool_contract_task_ledger.harn.prompt",
607        source: include_str!("stdlib/agent/prompts/tool_contract_task_ledger.harn.prompt"),
608    },
609    StdlibPromptAsset {
610        path: "agent/prompts/tool_contract_deferred_tools.harn.prompt",
611        source: include_str!("stdlib/agent/prompts/tool_contract_deferred_tools.harn.prompt"),
612    },
613    StdlibPromptAsset {
614        path: "agent/prompts/deferred_tool_listing.harn.prompt",
615        source: include_str!("stdlib/agent/prompts/deferred_tool_listing.harn.prompt"),
616    },
617    StdlibPromptAsset {
618        path: "agent/prompts/action_turn_nudge.harn.prompt",
619        source: include_str!("stdlib/agent/prompts/action_turn_nudge.harn.prompt"),
620    },
621    StdlibPromptAsset {
622        path: "agent/prompts/agent_turn_preamble.harn.prompt",
623        source: include_str!("stdlib/agent/prompts/agent_turn_preamble.harn.prompt"),
624    },
625    StdlibPromptAsset {
626        path: "agent/prompts/default_nudge.harn.prompt",
627        source: include_str!("stdlib/agent/prompts/default_nudge.harn.prompt"),
628    },
629    StdlibPromptAsset {
630        path: "agent/prompts/agentic_user_system.harn.prompt",
631        source: include_str!("stdlib/agent/prompts/agentic_user_system.harn.prompt"),
632    },
633    StdlibPromptAsset {
634        path: "agent/prompts/agentic_user_user.harn.prompt",
635        source: include_str!("stdlib/agent/prompts/agentic_user_user.harn.prompt"),
636    },
637    StdlibPromptAsset {
638        path: "agent/prompts/loop_until_done_system.harn.prompt",
639        source: include_str!("stdlib/agent/prompts/loop_until_done_system.harn.prompt"),
640    },
641    StdlibPromptAsset {
642        path: "agent/prompts/completion_judge_default.harn.prompt",
643        source: include_str!("stdlib/agent/prompts/completion_judge_default.harn.prompt"),
644    },
645    StdlibPromptAsset {
646        path: "agent/prompts/completion_judge_feedback_fallback.harn.prompt",
647        source: include_str!("stdlib/agent/prompts/completion_judge_feedback_fallback.harn.prompt"),
648    },
649    StdlibPromptAsset {
650        path: "agent/prompts/completion_judge_user.harn.prompt",
651        source: include_str!("stdlib/agent/prompts/completion_judge_user.harn.prompt"),
652    },
653    StdlibPromptAsset {
654        path: "agent/prompts/parse_guidance.harn.prompt",
655        source: include_str!("stdlib/agent/prompts/parse_guidance.harn.prompt"),
656    },
657    StdlibPromptAsset {
658        path: "agent/prompts/protocol_violation_feedback.harn.prompt",
659        source: include_str!("stdlib/agent/prompts/protocol_violation_feedback.harn.prompt"),
660    },
661    StdlibPromptAsset {
662        path: "agent/prompts/native_tool_contract_feedback.harn.prompt",
663        source: include_str!("stdlib/agent/prompts/native_tool_contract_feedback.harn.prompt"),
664    },
665    StdlibPromptAsset {
666        path: "agent/prompts/verification_gate_feedback.harn.prompt",
667        source: include_str!("stdlib/agent/prompts/verification_gate_feedback.harn.prompt"),
668    },
669    StdlibPromptAsset {
670        path: "agent/prompts/action_required_feedback.harn.prompt",
671        source: include_str!("stdlib/agent/prompts/action_required_feedback.harn.prompt"),
672    },
673    StdlibPromptAsset {
674        path: "agent/prompts/daemon_watch_feedback.harn.prompt",
675        source: include_str!("stdlib/agent/prompts/daemon_watch_feedback.harn.prompt"),
676    },
677    StdlibPromptAsset {
678        path: "agent/prompts/daemon_timer_feedback.harn.prompt",
679        source: include_str!("stdlib/agent/prompts/daemon_timer_feedback.harn.prompt"),
680    },
681    StdlibPromptAsset {
682        path: "llm/prompts/completion_fallback_system.harn.prompt",
683        source: include_str!("stdlib/llm/prompts/completion_fallback_system.harn.prompt"),
684    },
685    StdlibPromptAsset {
686        path: "llm/prompts/completion_fallback_user.harn.prompt",
687        source: include_str!("stdlib/llm/prompts/completion_fallback_user.harn.prompt"),
688    },
689    StdlibPromptAsset {
690        path: "llm/prompts/transcript_summarize_user.harn.prompt",
691        source: include_str!("stdlib/llm/prompts/transcript_summarize_user.harn.prompt"),
692    },
693    StdlibPromptAsset {
694        path: "llm/prompts/structural_chain_of_draft.harn.prompt",
695        source: include_str!("stdlib/llm/prompts/structural_chain_of_draft.harn.prompt"),
696    },
697    StdlibPromptAsset {
698        path: "llm/prompts/schema_recover_repair.harn.prompt",
699        source: include_str!("stdlib/llm/prompts/schema_recover_repair.harn.prompt"),
700    },
701    StdlibPromptAsset {
702        path: "llm/prompts/structured_envelope_schema_contract.harn.prompt",
703        source: include_str!("stdlib/llm/prompts/structured_envelope_schema_contract.harn.prompt"),
704    },
705    StdlibPromptAsset {
706        path: "llm/prompts/structured_envelope_repair.harn.prompt",
707        source: include_str!("stdlib/llm/prompts/structured_envelope_repair.harn.prompt"),
708    },
709    StdlibPromptAsset {
710        path: "llm/prompts/pairwise_rerank_user.harn.prompt",
711        source: include_str!("stdlib/llm/prompts/pairwise_rerank_user.harn.prompt"),
712    },
713    StdlibPromptAsset {
714        path: "llm/prompts/tool_binder_user.harn.prompt",
715        source: include_str!("stdlib/llm/prompts/tool_binder_user.harn.prompt"),
716    },
717    StdlibPromptAsset {
718        path: "workflow/prompts/stage.harn.prompt",
719        source: include_str!("stdlib/workflow/prompts/stage.harn.prompt"),
720    },
721    StdlibPromptAsset {
722        path: "workflow/prompts/verification_context_intro.harn.prompt",
723        source: include_str!("stdlib/workflow/prompts/verification_context_intro.harn.prompt"),
724    },
725    StdlibPromptAsset {
726        path: "orchestration/prompts/compaction_summary.harn.prompt",
727        source: include_str!("stdlib/orchestration/prompts/compaction_summary.harn.prompt"),
728    },
729];
730
731pub fn get_stdlib_source(module: &str) -> Option<&'static str> {
732    STDLIB_SOURCES
733        .iter()
734        .find_map(|entry| (entry.module == module).then_some(entry.source))
735}
736
737pub fn get_stdlib_prompt_asset(path: &str) -> Option<&'static str> {
738    let path = path.strip_prefix("std/").unwrap_or(path);
739    STDLIB_PROMPT_ASSETS
740        .iter()
741        .find_map(|entry| (entry.path == path).then_some(entry.source))
742}
743
744pub fn public_functions_for_module(module: &str) -> Vec<StdlibPublicFunction> {
745    let Some(source) = get_stdlib_source(module) else {
746        return Vec::new();
747    };
748    public_functions_from_source(source)
749}
750
751pub fn entrypoint_modules() -> Vec<StdlibEntrypointModule> {
752    STDLIB_SOURCES
753        .iter()
754        .filter_map(|entry| {
755            entrypoint_category_from_source(entry.source).map(|category| StdlibEntrypointModule {
756                import_path: format!("std/{}", entry.module),
757                category,
758            })
759        })
760        .collect()
761}
762
763fn entrypoint_category_from_source(source: &str) -> Option<String> {
764    for line in source.lines() {
765        let line = line.trim();
766        if line.is_empty() {
767            continue;
768        }
769        if let Some(category) = line.strip_prefix("// @harn-entrypoint-category ") {
770            let category = category.trim();
771            return (!category.is_empty()).then(|| category.to_string());
772        }
773        if !line.starts_with("//") {
774            return None;
775        }
776    }
777    None
778}
779
780fn public_functions_from_source(source: &str) -> Vec<StdlibPublicFunction> {
781    let mut out = Vec::new();
782    let mut doc: Option<String> = None;
783    let lines = source.lines().collect::<Vec<_>>();
784    let mut index = 0usize;
785    while index < lines.len() {
786        let line = lines[index].trim();
787        if line.starts_with("/**") {
788            let (parsed, next) = parse_harndoc(&lines, index);
789            doc = parsed;
790            index = next;
791            continue;
792        }
793        if line.starts_with("pub fn ") {
794            let (signature_line, next) = collect_public_function_signature(&lines, index);
795            if let Some(function) = parse_public_function_line(&signature_line, doc.take()) {
796                out.push(function);
797                index = next;
798                continue;
799            }
800        }
801        if let Some(function) = parse_public_function_line(line, doc.take()) {
802            out.push(function);
803        } else if !line.is_empty() && !line.starts_with("//") {
804            doc = None;
805        }
806        index += 1;
807    }
808    out
809}
810
811fn collect_public_function_signature(lines: &[&str], start: usize) -> (String, usize) {
812    let mut parts = Vec::new();
813    let mut index = start;
814    while index < lines.len() {
815        parts.push(lines[index].trim().to_string());
816        let candidate = parts.join(" ");
817        if public_function_signature_complete(&candidate) {
818            return (candidate, index + 1);
819        }
820        index += 1;
821    }
822    (parts.join(" "), index)
823}
824
825fn public_function_signature_complete(line: &str) -> bool {
826    let Some(rest) = line.strip_prefix("pub fn ") else {
827        return false;
828    };
829    let Some(name_end) = rest.find('(') else {
830        return false;
831    };
832    matching_paren_len(&rest[name_end + 1..]).is_some()
833}
834
835fn parse_harndoc(lines: &[&str], start: usize) -> (Option<String>, usize) {
836    let mut parts = Vec::new();
837    let mut index = start;
838    while index < lines.len() {
839        let mut line = lines[index].trim();
840        if index == start {
841            line = line.trim_start_matches("/**").trim();
842        }
843        let done = line.ends_with("*/");
844        line = line.trim_end_matches("*/").trim();
845        line = line.trim_start_matches('*').trim();
846        if !line.is_empty() {
847            parts.push(line.to_string());
848        }
849        index += 1;
850        if done {
851            break;
852        }
853    }
854    let text = parts.join("\n").trim().to_string();
855    ((!text.is_empty()).then_some(text), index)
856}
857
858fn parse_public_function_line(line: &str, doc: Option<String>) -> Option<StdlibPublicFunction> {
859    let rest = line.strip_prefix("pub fn ")?.trim();
860    let name_end = rest.find('(')?;
861    let name = rest[..name_end].trim();
862    if name.is_empty() {
863        return None;
864    }
865    let params_start = name_end + 1;
866    let params_len = matching_paren_len(&rest[params_start..])?;
867    let params = &rest[params_start..params_start + params_len];
868    let after = rest[params_start + params_len + 1..].trim();
869    let return_type = after
870        .strip_prefix("->")
871        .and_then(|tail| tail.split('{').next())
872        .map(str::trim)
873        .filter(|value| !value.is_empty());
874    let signature = match return_type {
875        Some(ret) => format!("{name}({params}) -> {ret}"),
876        None => format!("{name}({params})"),
877    };
878    let param_parts = split_top_level_params(params);
879    let total_params = param_parts
880        .iter()
881        .filter(|param| !param.trim().is_empty())
882        .count();
883    let variadic = param_parts
884        .iter()
885        .any(|param| param.trim_start().starts_with("..."));
886    let required_params = param_parts
887        .iter()
888        .filter(|param| {
889            let param = param.trim();
890            !param.is_empty() && !param.contains('=') && !param.starts_with("...")
891        })
892        .count();
893    Some(StdlibPublicFunction {
894        name: name.to_string(),
895        signature,
896        required_params,
897        total_params,
898        variadic,
899        doc,
900    })
901}
902
903fn matching_paren_len(input: &str) -> Option<usize> {
904    let mut depth = 1usize;
905    for (offset, ch) in input.char_indices() {
906        match ch {
907            '(' | '[' | '{' => depth += 1,
908            ')' => {
909                depth = depth.saturating_sub(1);
910                if depth == 0 {
911                    return Some(offset);
912                }
913            }
914            ']' | '}' => depth = depth.saturating_sub(1),
915            _ => {}
916        }
917    }
918    None
919}
920
921fn split_top_level_params(params: &str) -> Vec<&str> {
922    let mut out = Vec::new();
923    let mut depth = 0isize;
924    let mut start = 0usize;
925    for (offset, ch) in params.char_indices() {
926        match ch {
927            '(' | '[' | '{' => depth += 1,
928            ')' | ']' | '}' => depth -= 1,
929            ',' if depth == 0 => {
930                out.push(&params[start..offset]);
931                start = offset + 1;
932            }
933            _ => {}
934        }
935    }
936    out.push(&params[start..]);
937    out
938}
939
940#[cfg(test)]
941mod tests {
942    use std::collections::BTreeSet;
943
944    use super::{
945        entrypoint_modules, get_stdlib_prompt_asset, get_stdlib_source,
946        public_functions_for_module, STDLIB_PROMPT_ASSETS, STDLIB_SOURCES,
947    };
948
949    #[test]
950    fn stdlib_sources_are_non_empty() {
951        for entry in STDLIB_SOURCES {
952            assert!(
953                !entry.source.trim().is_empty(),
954                "{} should have non-empty source",
955                entry.module
956            );
957        }
958    }
959
960    #[test]
961    fn stdlib_source_names_are_unique() {
962        let mut names = BTreeSet::new();
963        for entry in STDLIB_SOURCES {
964            assert!(names.insert(entry.module), "duplicate {}", entry.module);
965        }
966    }
967
968    #[test]
969    fn stdlib_prompt_assets_are_non_empty() {
970        for entry in STDLIB_PROMPT_ASSETS {
971            assert!(
972                !entry.source.trim().is_empty(),
973                "{} should have non-empty prompt asset source",
974                entry.path
975            );
976        }
977    }
978
979    #[test]
980    fn stdlib_prompt_asset_paths_are_unique() {
981        let mut paths = BTreeSet::new();
982        for entry in STDLIB_PROMPT_ASSETS {
983            assert!(paths.insert(entry.path), "duplicate {}", entry.path);
984        }
985    }
986
987    #[test]
988    fn key_stdlib_modules_resolve() {
989        for module in [
990            "context",
991            "context/maintenance",
992            "edit",
993            "artifact/web",
994            "command",
995            "waitpoint",
996            "llm/handlers",
997            "llm/tool_middleware",
998            "llm/tool_binder",
999            "llm/ensemble",
1000            "llm/rerank",
1001            "personas/prelude",
1002            "personas/bulletins",
1003            "agent/host_tools",
1004            "agent/user",
1005            "llm/optimize",
1006            "llm/judge",
1007            "llm/refine",
1008            "connectors/shared",
1009            "connectors/github",
1010            "connectors/linear",
1011            "connectors/notion",
1012            "connectors/slack",
1013            "triage",
1014            "dashboard/jobs",
1015            "ui_resource",
1016        ] {
1017            assert!(
1018                get_stdlib_source(module).is_some(),
1019                "std/{module} should resolve"
1020            );
1021        }
1022    }
1023
1024    #[test]
1025    fn key_stdlib_prompt_assets_resolve() {
1026        for path in [
1027            "std/agent/prompts/tool_contract_text.harn.prompt",
1028            "std/agent/prompts/action_turn_nudge.harn.prompt",
1029            "std/agent/prompts/completion_judge_default.harn.prompt",
1030            "std/workflow/prompts/stage.harn.prompt",
1031            "std/orchestration/prompts/compaction_summary.harn.prompt",
1032        ] {
1033            assert!(
1034                get_stdlib_prompt_asset(path).is_some(),
1035                "{path} should resolve"
1036            );
1037        }
1038    }
1039
1040    #[test]
1041    fn public_function_catalog_derives_signatures_from_harn_source() {
1042        let exports = public_functions_for_module("workflow/execute");
1043        assert_eq!(exports.len(), 1);
1044        assert_eq!(exports[0].name, "workflow_execute");
1045        assert_eq!(
1046            exports[0].signature,
1047            "workflow_execute(task, graph, artifacts = nil, options = nil)"
1048        );
1049        assert_eq!(exports[0].required_params, 2);
1050        assert_eq!(exports[0].total_params, 4);
1051    }
1052
1053    #[test]
1054    fn command_stdlib_module_exports_step_helpers() {
1055        let exports = public_functions_for_module("command")
1056            .into_iter()
1057            .map(|function| function.name)
1058            .collect::<BTreeSet<_>>();
1059        for name in [
1060            "command_run",
1061            "command_output_tail",
1062            "command_json",
1063            "command_json_step",
1064            "command_try",
1065            "command_step",
1066            "command_steps_append",
1067            "command_last_failed_step",
1068            "command_step_ref",
1069        ] {
1070            assert!(exports.contains(name), "std/command should export {name}");
1071        }
1072    }
1073
1074    #[test]
1075    fn async_stdlib_exports_predicate_backoff_name_only() {
1076        let exports = public_functions_for_module("async")
1077            .into_iter()
1078            .map(|function| function.name)
1079            .collect::<BTreeSet<_>>();
1080        assert!(
1081            exports.contains("retry_predicate_with_backoff"),
1082            "std/async should export retry_predicate_with_backoff"
1083        );
1084        assert!(
1085            !exports.contains("retry_with_backoff"),
1086            "std/async should not retain the old retry_with_backoff export"
1087        );
1088    }
1089
1090    #[test]
1091    fn signal_stdlib_module_exports_interrupt_helpers() {
1092        let exports = public_functions_for_module("signal")
1093            .into_iter()
1094            .map(|function| function.name)
1095            .collect::<BTreeSet<_>>();
1096        for name in [
1097            "on_interrupt",
1098            "off_interrupt",
1099            "interrupted",
1100            "with_interrupt",
1101        ] {
1102            assert!(exports.contains(name), "std/signal should export {name}");
1103        }
1104    }
1105
1106    #[test]
1107    fn git_stdlib_module_exports_local_wrappers() {
1108        let exports = public_functions_for_module("git")
1109            .into_iter()
1110            .map(|function| function.name)
1111            .collect::<BTreeSet<_>>();
1112        for name in [
1113            "git_run",
1114            "git_status",
1115            "git_current_branch",
1116            "git_log",
1117            "git_switch",
1118            "git_pull_ff_only",
1119            "git_find_tool",
1120            "git_run_tool",
1121            "git_tools",
1122            "git_toolbox_tools",
1123        ] {
1124            assert!(exports.contains(name), "std/git should export {name}");
1125        }
1126    }
1127
1128    #[test]
1129    fn agent_workers_exports_suspend_resume_wrappers() {
1130        let exports = public_functions_for_module("agent/workers");
1131        let suspend = exports
1132            .iter()
1133            .find(|function| function.name == "suspend_agent")
1134            .expect("std/agent/workers should export suspend_agent");
1135        assert_eq!(
1136            suspend.signature,
1137            "suspend_agent(worker, reason = \"\", options = nil)"
1138        );
1139        assert_eq!(suspend.required_params, 1);
1140        assert_eq!(suspend.total_params, 3);
1141
1142        let resume = exports
1143            .iter()
1144            .find(|function| function.name == "resume_agent")
1145            .expect("std/agent/workers should export resume_agent");
1146        assert_eq!(
1147            resume.signature,
1148            "resume_agent(worker_or_snapshot, resume_input = nil, continue_transcript = true)"
1149        );
1150        assert_eq!(resume.required_params, 1);
1151        assert_eq!(resume.total_params, 3);
1152
1153        let parse_resume = exports
1154            .iter()
1155            .find(|function| function.name == "parse_resume_conditions")
1156            .expect("std/agent/workers should export parse_resume_conditions");
1157        assert_eq!(
1158            parse_resume.signature,
1159            "parse_resume_conditions(conditions = nil) -> ResumeConditions?"
1160        );
1161        assert_eq!(parse_resume.required_params, 0);
1162        assert_eq!(parse_resume.total_params, 1);
1163
1164        let lifecycle = exports
1165            .iter()
1166            .find(|function| function.name == "agent_lifecycle_tools")
1167            .expect("std/agent/workers should export agent_lifecycle_tools");
1168        assert_eq!(
1169            lifecycle.signature,
1170            "agent_lifecycle_tools(registry = nil, options = nil)"
1171        );
1172        assert_eq!(lifecycle.required_params, 0);
1173        assert_eq!(lifecycle.total_params, 2);
1174    }
1175
1176    #[test]
1177    fn tui_stdlib_module_exports_terminal_helpers() {
1178        let exports = public_functions_for_module("tui")
1179            .into_iter()
1180            .map(|function| function.name)
1181            .collect::<BTreeSet<_>>();
1182        for name in ["page", "terminal_width", "rule", "clear", "select_from"] {
1183            assert!(exports.contains(name), "std/tui should export {name}");
1184        }
1185    }
1186
1187    #[test]
1188    fn harn_entrypoint_catalog_is_declared_by_stdlib_sources() {
1189        let modules = entrypoint_modules();
1190        let entries = modules
1191            .iter()
1192            .map(|module| (module.import_path.as_str(), module.category.as_str()))
1193            .collect::<BTreeSet<_>>();
1194        for entry in [
1195            ("std/agent/loop", "agent.stdlib"),
1196            ("std/agent/turn", "agent.stdlib"),
1197            ("std/agent/primitives", "agent.stdlib"),
1198            ("std/workflow/execute", "workflow.stdlib"),
1199        ] {
1200            assert!(entries.contains(&entry), "{entry:?} should be declared");
1201        }
1202    }
1203}