pub const SOURCE: &str = include_str!("source.md");
pub fn extract_surface<'a>(source: &'a str, surface: &str) -> Option<&'a str> {
let open = format!("<!-- @surface {surface} -->");
let close = "<!-- @end -->";
let mut content_start: Option<usize> = None;
let mut cursor = 0usize;
for line in source.split_inclusive('\n') {
let line_start = cursor;
cursor += line.len();
let trimmed = line.trim_end_matches('\n').trim_end_matches('\r').trim();
match content_start {
None => {
if trimmed == open {
content_start = Some(cursor);
}
}
Some(_) => {
if trimmed == close {
return Some(&source[content_start.unwrap()..line_start]);
}
}
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_server_instructions_byte_for_byte() {
let extracted = extract_surface(SOURCE, "server_instructions")
.expect("server_instructions surface present in source.md");
assert_eq!(
extracted,
crate::prompts::SERVER_INSTRUCTIONS,
"source.md must reproduce server_instructions.md byte-for-byte; \
diff between extract and constant means source.md drifted from the file"
);
}
#[test]
fn extracts_onboarding_prompt_byte_for_byte() {
let extracted = extract_surface(SOURCE, "onboarding_prompt")
.expect("onboarding_prompt surface present in source.md");
assert_eq!(
extracted,
crate::prompts::RAW_ONBOARDING_PROMPT,
"source.md must reproduce onboarding_prompt.md byte-for-byte; \
diff between extract and constant means source.md drifted from the file"
);
}
#[test]
fn unknown_surface_returns_none() {
assert!(extract_surface(SOURCE, "no_such_surface").is_none());
}
#[test]
fn extract_handles_inline_string() {
let src = "<!-- @surface foo -->\nbody\n<!-- @end -->\n";
assert_eq!(extract_surface(src, "foo"), Some("body\n"));
}
#[test]
fn extract_handles_crlf_line_endings() {
let src = "<!-- @surface foo -->\r\nbody\r\n<!-- @end -->\r\n";
assert_eq!(extract_surface(src, "foo"), Some("body\r\n"));
}
#[test]
fn extract_returns_none_when_close_tag_missing() {
let src = "<!-- @surface foo -->\nbody without close tag\n";
assert!(extract_surface(src, "foo").is_none());
}
#[test]
fn extract_ignores_marker_quoted_in_prose() {
let src = "Editor note: never embed `<!-- @surface foo -->` literal text in prose.\n\
<!-- @surface foo -->\n\
real body\n\
<!-- @end -->\n";
assert_eq!(extract_surface(src, "foo"), Some("real body\n"));
}
#[test]
fn extract_ignores_close_marker_quoted_in_prose() {
let src = "<!-- @surface foo -->\n\
body line 1\n\
see the `<!-- @end -->` marker below for the actual end.\n\
body line 2\n\
<!-- @end -->\n";
let result = extract_surface(src, "foo").unwrap();
assert!(
result.contains("body line 2"),
"extractor terminated early at quoted close marker; got:\n{result}"
);
assert!(
result.contains("see the `<!-- @end -->` marker below"),
"quoted close marker line should be part of body, got:\n{result}"
);
}
#[test]
fn extract_tolerates_trailing_whitespace_on_marker() {
let src = "<!-- @surface foo --> \nbody\n<!-- @end --> \n";
assert_eq!(extract_surface(src, "foo"), Some("body\n"));
}
#[test]
fn extract_requires_marker_on_its_own_line() {
let src = "// <!-- @surface foo -->\nnot a real surface\n// <!-- @end -->\n";
assert!(
extract_surface(src, "foo").is_none(),
"marker prefixed by `// ` must not be matched as a real surface"
);
}
}