-
Notifications
You must be signed in to change notification settings - Fork 155
feat(cli): add support for NO_DNA agent mode (full no-dna.org complia… #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rit3sh-x
wants to merge
5
commits into
solana-foundation:main
Choose a base branch
from
rit3sh-x:feat/569-no-dna-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,149
−128
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01a629b
feat(cli): add support for NO_DNA agent mode (full no-dna.org complia…
rit3sh-x 3fbe007
Update crates/cli/src/agent_io.rs
rit3sh-x f2a9c72
Update crates/cli/src/cli/mod.rs
rit3sh-x 2f888d2
fix(cli): prevent ANSI leak into NO_DNA JSON msg field + restore huma…
rit3sh-x fd05985
fix(cli): dedupe NO_DNA JSONL emission from runbook log events
rit3sh-x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| //! Agent-mode I/O: JSONL emit for direct print sites under NO_DNA. | ||
| //! | ||
| //! Surfpool has two ways of producing user-visible output: | ||
| //! | ||
| //! 1. **Logger calls** (`info!`/`warn!`/`error!`) — flow through `fern` and | ||
| //! are formatted by the dispatch's format closure (see [`crate::cli::setup_logger`]). | ||
| //! 2. **Direct prints** (`println!`/`eprintln!`) — bypass the logger entirely. | ||
| //! | ||
| //! Under NO_DNA, both paths must produce the same JSONL contract. | ||
| //! The logger path is handled by the format closure; this module | ||
| //! handles direct prints via [`cli_emit`] and the `cli_info!`/`cli_warn!`/ | ||
| //! `cli_error!` macros. | ||
| //! | ||
| //! The JSON shape is `{"ts","level","target","msg"}` | ||
| //! - `ts` — RFC3339 UTC milliseconds | ||
| //! - `level` — lowercase string (`"trace"`/`"debug"`/`"info"`/`"warn"`/`"error"`) | ||
| //! - `target` — pseudo-event tag (caller-provided, e.g. `"surfpool.update.download"`) | ||
| //! - `msg` — `serde_json`-escaped string of the original human text | ||
| //! | ||
| //! Outside NO_DNA, `cli_emit` routes to `println!` (info/debug/trace) or | ||
| //! `eprintln!` (warn/error) — i.e. the legacy split human callers expect. | ||
|
|
||
| use chrono::{DateTime, SecondsFormat, Utc}; | ||
|
|
||
| use crate::no_dna::{AgentMode, OutputFormat}; | ||
|
|
||
| /// Format an agent-mode JSON record at the given timestamp. | ||
| pub fn format_agent_json_at(ts: DateTime<Utc>, level: &str, target: &str, msg: &str) -> String { | ||
| format_agent_json_with_data_at(ts, level, target, msg, None) | ||
| } | ||
|
|
||
| /// Like [`format_agent_json_at`] but optionally includes a `data` object for | ||
| /// structured payloads (used by `surfpool ls` to attach runbook metadata — | ||
| pub fn format_agent_json_with_data_at( | ||
| ts: DateTime<Utc>, | ||
| level: &str, | ||
| target: &str, | ||
| msg: &str, | ||
| data: Option<serde_json::Value>, | ||
| ) -> String { | ||
| let mut obj = serde_json::Map::with_capacity(5); | ||
| obj.insert( | ||
| "ts".into(), | ||
| serde_json::Value::String(ts.to_rfc3339_opts(SecondsFormat::Millis, true)), | ||
| ); | ||
| obj.insert("level".into(), serde_json::Value::String(level.to_string())); | ||
| obj.insert( | ||
| "target".into(), | ||
| serde_json::Value::String(target.to_string()), | ||
| ); | ||
| obj.insert("msg".into(), serde_json::Value::String(msg.to_string())); | ||
| if let Some(d) = data { | ||
| obj.insert("data".into(), d); | ||
| } | ||
| serde_json::Value::Object(obj).to_string() | ||
| } | ||
|
|
||
| /// Write a single JSONL line to stderr in agent-mode shape. | ||
| /// | ||
| /// Bypasses the `log` crate so this works even before `setup_logger` has | ||
| /// installed the fern dispatch. | ||
| pub fn agent_emit(level: &str, target: &str, msg: &str) { | ||
| eprintln!("{}", format_agent_json_at(Utc::now(), level, target, msg)); | ||
| } | ||
|
|
||
| /// Like [`agent_emit`] but attaches a `data` payload. Used by `surfpool ls` | ||
| /// to emit structured runbook records. | ||
| pub fn agent_emit_with_data(level: &str, target: &str, msg: &str, data: serde_json::Value) { | ||
| eprintln!( | ||
| "{}", | ||
| format_agent_json_with_data_at(Utc::now(), level, target, msg, Some(data)) | ||
| ); | ||
| } | ||
|
|
||
| /// Emit a CLI line, branching on agent mode. | ||
| /// | ||
| /// Under NO_DNA: JSON to stderr via [`agent_emit`]. | ||
| /// Outside NO_DNA: plain text — `stderr` for `warn`/`error`, `stdout` for | ||
| /// everything else (preserves the legacy split humans expect). | ||
| pub fn cli_emit(level: &str, target: &str, msg: &str) { | ||
| let mode = AgentMode::from_env(); | ||
| if mode.output_format == OutputFormat::Json { | ||
| agent_emit(level, target, msg); | ||
| } else if matches!(level, "warn" | "error") { | ||
| eprintln!("{msg}"); | ||
| } else { | ||
| println!("{msg}"); | ||
| } | ||
| } | ||
|
|
||
| /// `trace`-level direct emit. See [`cli_emit`]. | ||
| #[macro_export] | ||
| macro_rules! cli_trace { | ||
| ($target:literal, $($arg:tt)*) => { | ||
| $crate::agent_io::cli_emit("trace", $target, &format!($($arg)*)) | ||
| }; | ||
| } | ||
|
|
||
| /// `debug`-level direct emit. See [`cli_emit`]. | ||
| #[macro_export] | ||
| macro_rules! cli_debug { | ||
| ($target:literal, $($arg:tt)*) => { | ||
| $crate::agent_io::cli_emit("debug", $target, &format!($($arg)*)) | ||
| }; | ||
| } | ||
|
|
||
| /// `info`-level direct emit. See [`cli_emit`]. | ||
| #[macro_export] | ||
| macro_rules! cli_info { | ||
| ($target:literal, $($arg:tt)*) => { | ||
| $crate::agent_io::cli_emit("info", $target, &format!($($arg)*)) | ||
| }; | ||
| } | ||
|
|
||
| /// `warn`-level direct emit. See [`cli_emit`]. | ||
| #[macro_export] | ||
| macro_rules! cli_warn { | ||
| ($target:literal, $($arg:tt)*) => { | ||
| $crate::agent_io::cli_emit("warn", $target, &format!($($arg)*)) | ||
| }; | ||
| } | ||
|
|
||
| /// `error`-level direct emit. See [`cli_emit`]. | ||
| #[macro_export] | ||
| macro_rules! cli_error { | ||
| ($target:literal, $($arg:tt)*) => { | ||
| $crate::agent_io::cli_emit("error", $target, &format!($($arg)*)) | ||
| }; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use chrono::TimeZone; | ||
|
|
||
| use super::*; | ||
|
|
||
| // agent_emit output parses as JSON via serde_json; | ||
| // all required fields present; msg escaped. | ||
| #[test] | ||
| fn format_agent_json_is_valid_json() { | ||
| let ts = Utc.with_ymd_and_hms(2026, 6, 4, 17, 30, 0).unwrap(); | ||
| let s = format_agent_json_at(ts, "info", "surfpool.test", "hello world"); | ||
| let parsed: serde_json::Value = serde_json::from_str(&s).expect("must be valid JSON"); | ||
| assert_eq!(parsed["level"], "info"); | ||
| assert_eq!(parsed["target"], "surfpool.test"); | ||
| assert_eq!(parsed["msg"], "hello world"); | ||
| assert!(parsed["ts"].is_string()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn format_agent_json_escapes_msg() { | ||
| let ts = Utc.with_ymd_and_hms(2026, 6, 4, 17, 30, 0).unwrap(); | ||
| let s = format_agent_json_at(ts, "error", "surfpool.test", "line1\nline2 \"quoted\""); | ||
| let parsed: serde_json::Value = serde_json::from_str(&s).expect("must be valid JSON"); | ||
| assert_eq!(parsed["msg"], "line1\nline2 \"quoted\""); | ||
| // Raw string must NOT contain bare newlines or unescaped quotes. | ||
| assert!(!s.contains("line1\nline2"), "newlines must be escaped"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn timestamp_is_rfc3339_utc_with_z_suffix() { | ||
| let ts = Utc.with_ymd_and_hms(2026, 6, 4, 17, 30, 0).unwrap(); | ||
| let s = format_agent_json_at(ts, "info", "t", "m"); | ||
| let parsed: serde_json::Value = serde_json::from_str(&s).unwrap(); | ||
| let ts_str = parsed["ts"].as_str().unwrap(); | ||
| assert!( | ||
| ts_str.ends_with('Z'), | ||
| "RFC3339 UTC must end with Z, got {ts_str}" | ||
| ); | ||
| assert!( | ||
| ts_str.starts_with("2026-06-04T17:30:00"), | ||
| "expected fixed timestamp, got {ts_str}" | ||
| ); | ||
| // Re-parse to confirm round-trip | ||
| let _: DateTime<Utc> = ts_str.parse().expect("must round-trip parse"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn agent_json_contains_no_ansi_escapes() { | ||
| let ts = Utc.with_ymd_and_hms(2026, 6, 4, 17, 30, 0).unwrap(); | ||
| // Pass a message that COULD contain colors if we accidentally let them through. | ||
| let s = format_agent_json_at(ts, "warn", "surfpool.test", "plain msg"); | ||
| assert!( | ||
| !s.contains('\x1b'), | ||
| "JSON output must contain no ANSI ESC byte (0x1b)" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn format_agent_json_handles_unicode_in_msg() { | ||
| let ts = Utc.with_ymd_and_hms(2026, 6, 4, 17, 30, 0).unwrap(); | ||
| let s = format_agent_json_at(ts, "info", "t", "✓ done — 日本語"); | ||
| let parsed: serde_json::Value = serde_json::from_str(&s).expect("must be valid JSON"); | ||
| assert_eq!(parsed["msg"], "✓ done — 日本語"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.