Skip to main content

agent_doc/
lib.rs

1//! # Module: lib (agent_doc)
2//!
3//! ## Spec
4//! - Exposes the public API surface consumed by the CLI binary, FFI layer, and editor plugins.
5//! - Re-exports: `component`, `crdt`, `debounce`, `ffi`, `frontmatter`, `merge`, `template`.
6//! - Provides two boundary-ID utilities used across all write paths:
7//!   - `new_boundary_id()` — 8-hex-char UUID v4 prefix (length controlled by `BOUNDARY_ID_LEN`).
8//!   - `new_boundary_id_with_summary(summary)` — same ID optionally suffixed with a 3-word,
9//!     20-char-max slug derived from `summary` (format: `a0cfeb34:boundary-fix`).
10//! - `format_boundary_marker(id)` — renders the canonical HTML comment form
11//!   `<!-- agent:boundary:<id> -->` used in document component boundaries.
12//! - `BOUNDARY_ID_LEN = 8` is a public constant; callers may read but should not override.
13//!
14//! ## Agentic Contracts
15//! - All public symbols are safe to call from FFI consumers (JNA, napi-rs) via `ffi` module.
16//! - `new_boundary_id` is non-deterministic (UUID v4); callers must not rely on ordering.
17//! - `new_boundary_id_with_summary(None)` and `new_boundary_id_with_summary(Some(""))` both
18//!   return a plain 8-char ID with no suffix.
19//! - Slug derivation: lowercase, non-alphanumeric chars → `-`, collapse runs, take first 3 words,
20//!   truncate to 20 chars.
21//!
22//! ## Evals
23//! - new_boundary_id_len: result is exactly `BOUNDARY_ID_LEN` chars of hex
24//! - new_boundary_id_with_summary_none: `None` summary → plain 8-char hex
25//! - new_boundary_id_with_summary_empty: `Some("")` → plain 8-char hex
26//! - new_boundary_id_with_summary_slug: `Some("Boundary Fix")` → `"<id>:boundary-fix"`
27//! - new_boundary_id_with_summary_truncate: long summary → slug capped at 20 chars
28//! - format_boundary_marker: `"abc123"` → `"<!-- agent:boundary:abc123 -->"`
29
30pub mod component;
31pub mod crdt;
32pub mod debounce;
33pub mod ffi;
34pub mod frontmatter;
35pub mod ipc_socket;
36pub mod merge;
37pub mod template;
38
39/// Default number of hex characters for boundary IDs.
40pub const BOUNDARY_ID_LEN: usize = 8;
41
42/// Generate a new boundary ID (short hex string from UUID v4).
43pub fn new_boundary_id() -> String {
44    let full = uuid::Uuid::new_v4().to_string().replace('-', "");
45    full[..BOUNDARY_ID_LEN.min(full.len())].to_string()
46}
47
48/// Generate a boundary ID with an optional summary suffix.
49///
50/// Format: `a0cfeb34` or `a0cfeb34:boundary-fix` (with summary).
51/// The summary is slugified (lowercase, non-alphanumeric → `-`, max 20 chars).
52pub fn new_boundary_id_with_summary(summary: Option<&str>) -> String {
53    let id = new_boundary_id();
54    match summary {
55        Some(s) if !s.is_empty() => {
56            let slug: String = s.to_lowercase()
57                .chars()
58                .map(|c| if c.is_alphanumeric() { c } else { '-' })
59                .collect::<String>()
60                .split('-')
61                .filter(|s| !s.is_empty())
62                .take(3) // max 3 words
63                .collect::<Vec<&str>>()
64                .join("-");
65            let slug = &slug[..slug.len().min(20)];
66            format!("{}:{}", id, slug)
67        }
68        _ => id,
69    }
70}
71
72/// Format a boundary marker comment.
73pub fn format_boundary_marker(id: &str) -> String {
74    format!("<!-- agent:boundary:{} -->", id)
75}