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