Files
Kigi-CLI/crates/common/kigi-interjection-core/src/format.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

61 lines
1.8 KiB
Rust

/// Truncation threshold; matches the shell's large-prompt limit.
pub const LARGE_PROMPT_THRESHOLD: usize = 25_000;
/// Wrap a user message in the canonical `<user_query>` envelope.
pub fn user_query(user_message: &str) -> String {
format!(
r#"<user_query>
{user_message}
</user_query>"#
)
}
/// Frame interjection text as a synthetic mid-turn user message.
/// No deferral instruction — the model weighs it against in-flight work.
/// Byte-identical to the shell's historical format.
pub fn format_interjection(text: String) -> String {
let truncated = if text.len() > LARGE_PROMPT_THRESHOLD {
let end = text
.char_indices()
.take_while(|(i, _)| *i < LARGE_PROMPT_THRESHOLD)
.last()
.map(|(i, c)| i + c.len_utf8())
.unwrap_or(text.len());
format!("{}... [truncated]", &text[..end])
} else {
text
};
format!(
"The user sent a message while you were working:\n{}",
user_query(&truncated)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wraps_in_user_query_with_midturn_note() {
let out = format_interjection("stop and fix the test first".into());
assert!(out.starts_with("The user sent a message while you were working:\n<user_query>\n"));
assert!(out.ends_with("\n</user_query>"));
assert!(out.contains("stop and fix the test first"));
}
#[test]
fn truncates_at_utf8_boundary() {
let s = "é".repeat(LARGE_PROMPT_THRESHOLD);
let out = format_interjection(s);
assert!(out.contains("... [truncated]"));
assert!(out.len() < LARGE_PROMPT_THRESHOLD + 200);
}
#[test]
fn short_text_untouched() {
let out = format_interjection("hi".into());
assert!(!out.contains("[truncated]"));
}
}