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