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