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