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