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