pub mod patterns {
pub const ROOT: &str = "smith";
pub mod intents {
pub const RAW: &str = "smith.intents.raw";
pub const VETTED: &str = "smith.intents.vetted";
pub const QUARANTINE: &str = "smith.intents.quarantine";
pub const ALL: &str = "smith.intents.>";
pub const FS_READ_V1: &str = "smith.intents.fs.read.v1";
pub const HTTP_FETCH_V1: &str = "smith.intents.http.fetch.v1";
pub const SHELL_EXEC_V1: &str = "smith.intents.shell.exec.v1";
pub const RAW_PLAYBOOK: &str = "smith.intents.raw.playbook";
pub const RAW_PLAYBOOK_ALL: &str = "smith.intents.raw.playbook.*";
pub const RAW_MACRO: &str = "smith.intents.raw.macro";
pub const RAW_MACRO_ALL: &str = "smith.intents.raw.macro.*";
pub const RAW_ATOM_USE: &str = "smith.intents.raw.atom.use";
}
pub mod results {
pub const ALL: &str = "smith.results.>";
pub const PREFIX: &str = "smith.results";
}
pub mod events {
pub const ALL: &str = "smith.events.>";
pub const EXECUTOR_LIFECYCLE: &str = "smith.events.executor.lifecycle";
pub const HEALTH: &str = "smith.events.health";
pub const METRICS: &str = "smith.events.metrics";
}
pub mod logs {
pub const ALL: &str = "smith.logs.>";
pub const CORE: &str = "smith.logs.core";
pub const CORE_ALL: &str = "smith.logs.core.*";
pub const EXECUTOR: &str = "smith.logs.executor";
pub const EXECUTOR_ALL: &str = "smith.logs.executor.*";
pub const ADMISSION: &str = "smith.logs.admission";
pub const ADMISSION_ALL: &str = "smith.logs.admission.*";
pub const HTTP: &str = "smith.logs.http";
pub const HTTP_ALL: &str = "smith.logs.http.*";
pub const ERRORS: &str = "smith.logs.errors";
pub const ERRORS_ALL: &str = "smith.logs.errors.*";
pub const PERFORMANCE: &str = "smith.logs.performance";
pub const PERFORMANCE_ALL: &str = "smith.logs.performance.*";
pub const SECURITY: &str = "smith.logs.security";
pub const SECURITY_ALL: &str = "smith.logs.security.*";
}
pub mod audit {
pub const ALL: &str = "smith.audit.>";
pub const EXECUTION: &str = "smith.audit.execution";
pub const POLICY: &str = "smith.audit.policy";
pub const POLICY_ALL: &str = "smith.audit.policy.*";
pub const SECURITY: &str = "smith.audit.security";
}
pub mod ai_sdlc {
pub const ALL: &str = "smith.ai.sdlc.>";
pub const ACTION_REQUEST: &str = "smith.ai.sdlc.action.request";
pub const ACTION_RESULT: &str = "smith.ai.sdlc.action.result";
}
pub mod gitlab {
pub const ALL: &str = "smith.gitlab.>";
pub const EVENT_MR: &str = "smith.gitlab.event.mr";
pub const EVENT_PIPELINE: &str = "smith.gitlab.event.pipeline";
pub const EVENT_ISSUE: &str = "smith.gitlab.event.issue";
}
pub mod orchestrator {
pub const ALL: &str = "smith.orchestrator.>";
pub const AUDIT: &str = "smith.orchestrator.audit";
pub const STATUS: &str = "smith.orchestrator.status";
}
pub mod benchmark {
pub const ALL: &str = "smith.benchmark.>";
pub const RUNS: &str = "smith.benchmark.runs";
pub const STEPS: &str = "smith.benchmark.steps";
pub const TOOL_PERFORMANCE: &str = "smith.benchmark.tools";
pub const CONTEXT_DECISIONS: &str = "smith.benchmark.context";
pub const FAILURE_ANALYSIS: &str = "smith.benchmark.failures";
pub const OPTIMIZER_FEEDBACK: &str = "smith.benchmark.optimizer";
pub const TASK_FEATURES: &str = "smith.benchmark.task_features";
pub const EARLY_STOPPING: &str = "smith.benchmark.early_stop";
pub const POLICY_ASSIGNMENTS: &str = "smith.benchmark.policy_assignments";
pub const REGRESSION_ALERTS: &str = "smith.benchmark.regressions";
}
}
pub struct SubjectBuilder {
parts: Vec<String>,
}
impl SubjectBuilder {
pub fn new() -> Self {
Self {
parts: vec!["smith".to_string()],
}
}
pub fn part<S: AsRef<str>>(mut self, part: S) -> Self {
self.parts.push(part.as_ref().to_string());
self
}
pub fn capability<S: AsRef<str>>(mut self, capability: S) -> Self {
let cap = capability.as_ref();
if cap.contains('.') {
self.parts.push(cap.to_string());
} else {
self.parts.push(format!("{}.v1", cap));
}
self
}
pub fn domain<S: AsRef<str>>(mut self, domain: S) -> Self {
self.parts.push(domain.as_ref().to_string());
self
}
pub fn build(self) -> String {
self.parts.join(".")
}
pub fn wildcard(mut self) -> String {
self.parts.push(">".to_string());
self.parts.join(".")
}
}
impl Default for SubjectBuilder {
fn default() -> Self {
Self::new()
}
}
pub mod builders {
use super::SubjectBuilder;
pub struct IntentSubject;
impl IntentSubject {
pub fn raw<S: AsRef<str>>(capability: S) -> String {
SubjectBuilder::new()
.part("intents")
.part("raw")
.capability(capability)
.build()
}
pub fn vetted<S: AsRef<str>>(capability: S) -> String {
SubjectBuilder::new()
.part("intents")
.part("vetted")
.capability(capability)
.build()
}
pub fn with_domain<S: AsRef<str>>(capability: S, domain: S) -> String {
SubjectBuilder::new()
.part("intents")
.capability(capability)
.domain(domain)
.build()
}
pub fn quarantine() -> String {
SubjectBuilder::new()
.part("intents")
.part("quarantine")
.build()
}
}
pub struct ResultSubject;
impl ResultSubject {
pub fn for_intent<S: AsRef<str>>(intent_id: S) -> String {
SubjectBuilder::new()
.part("results")
.part(intent_id.as_ref())
.build()
}
pub fn all() -> String {
SubjectBuilder::new().part("results").wildcard()
}
}
pub struct EventSubject;
impl EventSubject {
pub fn executor_lifecycle<S: AsRef<str>>(executor_id: S) -> String {
SubjectBuilder::new()
.part("events")
.part("executor")
.part("lifecycle")
.part(executor_id.as_ref())
.build()
}
pub fn health<S: AsRef<str>>(service: S) -> String {
SubjectBuilder::new()
.part("events")
.part("health")
.part(service.as_ref())
.build()
}
pub fn metrics<S: AsRef<str>>(service: S) -> String {
SubjectBuilder::new()
.part("events")
.part("metrics")
.part(service.as_ref())
.build()
}
}
pub struct AuditSubject;
impl AuditSubject {
pub fn execution<S: AsRef<str>>(intent_id: S) -> String {
SubjectBuilder::new()
.part("audit")
.part("execution")
.part(intent_id.as_ref())
.build()
}
pub fn policy<S: AsRef<str>>(intent_id: S) -> String {
SubjectBuilder::new()
.part("audit")
.part("policy")
.part(intent_id.as_ref())
.build()
}
pub fn security<S: AsRef<str>>(event_type: S) -> String {
SubjectBuilder::new()
.part("audit")
.part("security")
.part(event_type.as_ref())
.build()
}
}
pub struct LogSubject;
impl LogSubject {
pub fn core<S: AsRef<str>>(level: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("core")
.part(level.as_ref())
.build()
}
pub fn executor<S: AsRef<str>>(level: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("executor")
.part(level.as_ref())
.build()
}
pub fn admission<S: AsRef<str>>(level: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("admission")
.part(level.as_ref())
.build()
}
pub fn http<S: AsRef<str>>(level: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("http")
.part(level.as_ref())
.build()
}
pub fn service<S: AsRef<str>>(service: S, level: S) -> String {
SubjectBuilder::new()
.part("logs")
.part(service.as_ref())
.part(level.as_ref())
.build()
}
pub fn error<S: AsRef<str>>(service: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("errors")
.part(service.as_ref())
.build()
}
pub fn performance<S: AsRef<str>>(service: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("performance")
.part(service.as_ref())
.build()
}
pub fn security<S: AsRef<str>>(service: S, event_type: S) -> String {
SubjectBuilder::new()
.part("logs")
.part("security")
.part(service.as_ref())
.part(event_type.as_ref())
.build()
}
pub fn all() -> String {
SubjectBuilder::new().part("logs").wildcard()
}
}
pub struct BenchmarkSubject;
impl BenchmarkSubject {
pub fn run<S: AsRef<str>>(run_id: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("runs")
.part(run_id.as_ref())
.build()
}
pub fn step<S: AsRef<str>>(run_id: S, step_id: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("steps")
.part(run_id.as_ref())
.part(step_id.as_ref())
.build()
}
pub fn tool_performance<S: AsRef<str>>(run_id: S, tool_name: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("tools")
.part(run_id.as_ref())
.part(tool_name.as_ref())
.build()
}
pub fn context_decision<S: AsRef<str>>(run_id: S, segment_id: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("context")
.part(run_id.as_ref())
.part(segment_id.as_ref())
.build()
}
pub fn failure<S: AsRef<str>>(run_id: S, failure_type: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("failures")
.part(run_id.as_ref())
.part(failure_type.as_ref())
.build()
}
pub fn optimizer_feedback<S: AsRef<str>>(task_type: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("optimizer")
.part(task_type.as_ref())
.build()
}
pub fn task_features<S: AsRef<str>>(task_id: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("task_features")
.part(task_id.as_ref())
.build()
}
pub fn early_stop<S: AsRef<str>>(run_id: S, reason: S) -> String {
SubjectBuilder::new()
.part("benchmark")
.part("early_stop")
.part(run_id.as_ref())
.part(reason.as_ref())
.build()
}
pub fn all() -> String {
SubjectBuilder::new().part("benchmark").wildcard()
}
}
}
pub fn raw<S: AsRef<str>>(capability: S) -> String {
builders::IntentSubject::raw(capability)
}
pub fn vetted<S: AsRef<str>>(capability: S) -> String {
builders::IntentSubject::vetted(capability)
}
pub mod streams {
pub const INTENTS: &str = "INTENTS";
pub const INTENTS_RAW: &str = "INTENTS_RAW";
pub const INTENTS_VETTED: &str = "INTENTS_VETTED";
pub const INTENTS_QUARANTINE: &str = "INTENTS_QUARANTINE";
pub const RESULTS: &str = "RESULTS";
pub const EVENTS: &str = "EVENTS";
pub const SYSTEM_EVENTS: &str = "SYSTEM_EVENTS";
pub const AUDIT_LOGS: &str = "AUDIT_LOGS";
pub const BENCHMARK_RUNS: &str = "BENCHMARK_RUNS";
pub const BENCHMARK_STEPS: &str = "BENCHMARK_STEPS";
pub const BENCHMARK_TOOLS: &str = "BENCHMARK_TOOLS";
pub const BENCHMARK_CONTEXT: &str = "BENCHMARK_CONTEXT";
pub const BENCHMARK_FAILURES: &str = "BENCHMARK_FAILURES";
pub const BENCHMARK_OPTIMIZER: &str = "BENCHMARK_OPTIMIZER";
}
pub mod consumers {
pub const EXECUTOR: &str = "executor";
pub const EXECUTOR_FS_READ: &str = "executor-fs-read";
pub const EXECUTOR_HTTP_FETCH: &str = "executor-http-fetch";
pub const HTTP_RESULTS: &str = "http-results";
pub const HTTP_EVENTS: &str = "http-events";
pub const ADMISSION_RAW: &str = "admission-raw";
pub const AUDIT_COLLECTOR: &str = "audit-collector";
pub const BENCHMARK_COLLECTOR: &str = "benchmark-collector";
pub const BENCHMARK_ANALYTICS: &str = "benchmark-analytics";
pub const BENCHMARK_OPTIMIZER: &str = "benchmark-optimizer";
pub const BENCHMARK_DASHBOARD: &str = "benchmark-dashboard";
}
pub mod abi {
pub const SUBJECT_ABI_VERSION: u32 = 1;
pub fn generate_subject_abi_hash() -> String {
use sha2::{Digest, Sha256};
let subjects = [
super::patterns::intents::RAW,
super::patterns::intents::VETTED,
super::patterns::intents::QUARANTINE,
super::patterns::intents::FS_READ_V1,
super::patterns::intents::HTTP_FETCH_V1,
super::patterns::results::PREFIX,
super::patterns::events::HEALTH,
super::patterns::audit::EXECUTION,
];
let abi_repr = format!(
"SUBJECT_ABI_V{}_SUBJECTS:{}",
SUBJECT_ABI_VERSION,
subjects.join(",")
);
let mut hasher = Sha256::new();
hasher.update(abi_repr.as_bytes());
format!("{:x}", hasher.finalize())
}
pub fn validate_subject_abi_stability(old_hash: &str, new_hash: &str) -> Result<(), String> {
if old_hash != new_hash {
return Err(format!(
"Subject ABI hash mismatch: expected {} but got {}. This indicates breaking changes to NATS subjects.",
old_hash, new_hash
));
}
Ok(())
}
}
#[macro_export]
macro_rules! validate_no_raw_subjects {
($code:expr) => {
compile_error!("Raw subject strings are forbidden. Use smith_bus::subjects constants or builders instead.")
};
}
pub mod validation {
pub fn is_valid_smith_subject(subject: &str) -> bool {
subject.starts_with("smith.") && !subject.is_empty()
}
pub fn contains_raw_subject_patterns(code: &str) -> Vec<String> {
let mut violations = Vec::new();
let raw_patterns = vec![
r#""smith\.intents\."#,
r#""smith\.results\."#,
r#""smith\.events\."#,
r#""smith\.audit\."#,
];
for pattern in raw_patterns {
if let Ok(regex) = regex::Regex::new(pattern) {
for mat in regex.find_iter(code) {
violations.push(mat.as_str().to_string());
}
}
}
violations
}
pub fn validate_centralized_usage(code: &str) -> Result<(), String> {
let violations = contains_raw_subject_patterns(code);
if !violations.is_empty() {
return Err(format!(
"Found {} raw subject string(s): {}. Use smith_bus::subjects constants instead.",
violations.len(),
violations.join(", ")
));
}
Ok(())
}
pub fn is_intent_subject(subject: &str) -> bool {
subject.starts_with("smith.intents.")
}
pub fn is_result_subject(subject: &str) -> bool {
subject.starts_with("smith.results.")
}
pub fn extract_capability(subject: &str) -> Option<String> {
if !is_intent_subject(subject) {
return None;
}
let parts: Vec<&str> = subject.split('.').collect();
if parts.len() >= 4 {
Some(format!("{}.{}", parts[2], parts[3]))
} else {
None
}
}
pub fn extract_intent_id(subject: &str) -> Option<String> {
if !is_result_subject(subject) {
return None;
}
let parts: Vec<&str> = subject.split('.').collect();
if parts.len() >= 3 {
Some(parts[2].to_string())
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_subject_builder() {
let subject = SubjectBuilder::new()
.part("intents")
.capability("fs.read.v1")
.domain("file-processor")
.build();
assert_eq!(subject, "smith.intents.fs.read.v1.file-processor");
}
#[test]
fn test_intent_subject_builders() {
assert_eq!(
builders::IntentSubject::raw("fs.read.v1"),
"smith.intents.raw.fs.read.v1"
);
assert_eq!(
builders::IntentSubject::vetted("http.fetch.v1"),
"smith.intents.vetted.http.fetch.v1"
);
assert_eq!(
builders::IntentSubject::with_domain("fs.read.v1", "processor"),
"smith.intents.fs.read.v1.processor"
);
}
#[test]
fn test_result_subject_builders() {
let intent_id = "intent-123-456";
assert_eq!(
builders::ResultSubject::for_intent(intent_id),
"smith.results.intent-123-456"
);
assert_eq!(builders::ResultSubject::all(), "smith.results.>");
}
#[test]
fn test_convenience_functions() {
assert_eq!(raw("fs.read.v1"), "smith.intents.raw.fs.read.v1");
assert_eq!(
vetted("http.fetch.v1"),
"smith.intents.vetted.http.fetch.v1"
);
}
#[test]
fn test_subject_validation() {
assert!(validation::is_valid_smith_subject(
"smith.intents.fs.read.v1"
));
assert!(!validation::is_valid_smith_subject(
"nats.intents.fs.read.v1"
));
assert!(validation::is_intent_subject("smith.intents.fs.read.v1"));
assert!(!validation::is_intent_subject("smith.results.123"));
assert!(validation::is_result_subject("smith.results.intent-123"));
assert!(!validation::is_result_subject("smith.intents.fs.read.v1"));
}
#[test]
fn test_capability_extraction() {
assert_eq!(
validation::extract_capability("smith.intents.fs.read.v1"),
Some("fs.read".to_string())
);
assert_eq!(
validation::extract_capability("smith.intents.http.fetch.v1"),
Some("http.fetch".to_string())
);
assert_eq!(validation::extract_capability("smith.results.123"), None);
}
#[test]
fn test_intent_id_extraction() {
assert_eq!(
validation::extract_intent_id("smith.results.intent-123-456"),
Some("intent-123-456".to_string())
);
assert_eq!(
validation::extract_intent_id("smith.intents.fs.read.v1"),
None
);
}
#[test]
fn test_stream_constants() {
assert_eq!(streams::INTENTS, "INTENTS");
assert_eq!(streams::RESULTS, "RESULTS");
assert_eq!(streams::AUDIT_LOGS, "AUDIT_LOGS");
}
#[test]
fn test_consumer_constants() {
assert_eq!(consumers::EXECUTOR, "executor");
assert_eq!(consumers::HTTP_RESULTS, "http-results");
assert_eq!(consumers::ADMISSION_RAW, "admission-raw");
}
#[test]
fn test_subject_prefix_contracts() {
let mut all_subjects = vec![
patterns::intents::RAW.to_string(),
patterns::intents::VETTED.to_string(),
patterns::intents::QUARANTINE.to_string(),
patterns::intents::FS_READ_V1.to_string(),
patterns::intents::HTTP_FETCH_V1.to_string(),
patterns::results::PREFIX.to_string(),
patterns::events::EXECUTOR_LIFECYCLE.to_string(),
patterns::events::HEALTH.to_string(),
patterns::events::METRICS.to_string(),
patterns::audit::EXECUTION.to_string(),
patterns::audit::POLICY.to_string(),
patterns::audit::SECURITY.to_string(),
patterns::ai_sdlc::ACTION_REQUEST.to_string(),
patterns::ai_sdlc::ACTION_RESULT.to_string(),
patterns::gitlab::EVENT_MR.to_string(),
patterns::gitlab::EVENT_PIPELINE.to_string(),
patterns::gitlab::EVENT_ISSUE.to_string(),
patterns::orchestrator::AUDIT.to_string(),
patterns::orchestrator::STATUS.to_string(),
];
all_subjects.extend([
builders::IntentSubject::raw("fs.read.v1"),
builders::IntentSubject::vetted("http.fetch.v1"),
builders::IntentSubject::with_domain("fs.read.v1", "test"),
builders::IntentSubject::quarantine(),
builders::ResultSubject::for_intent("test-intent"),
builders::EventSubject::executor_lifecycle("test-executor"),
builders::EventSubject::health("test-service"),
builders::EventSubject::metrics("test-service"),
builders::AuditSubject::execution("test-intent"),
builders::AuditSubject::policy("test-intent"),
builders::AuditSubject::security("test-event"),
raw("fs.read.v1"),
vetted("http.fetch.v1"),
]);
for subject in &all_subjects {
assert!(
subject.starts_with("smith."),
"Subject '{}' does not start with 'smith.' prefix",
subject
);
assert!(
!subject.contains("{}") && !subject.contains("{") && !subject.contains("}"),
"Subject '{}' contains template patterns - this indicates a bare string wasn't replaced",
subject
);
}
let intent_subjects: Vec<&String> = all_subjects
.iter()
.filter(|s| s.contains(".intents."))
.collect();
let result_subjects: Vec<&String> = all_subjects
.iter()
.filter(|s| s.contains(".results."))
.collect();
let event_subjects: Vec<&String> = all_subjects
.iter()
.filter(|s| s.contains(".events."))
.collect();
let audit_subjects: Vec<&String> = all_subjects
.iter()
.filter(|s| s.contains(".audit."))
.collect();
assert!(
intent_subjects.len() >= 6,
"Should have at least 6 intent subjects"
);
assert!(
!result_subjects.is_empty(),
"Should have at least 1 result subject"
);
assert!(
event_subjects.len() >= 3,
"Should have at least 3 event subjects"
);
assert!(
audit_subjects.len() >= 3,
"Should have at least 3 audit subjects"
);
for intent_subject in &intent_subjects {
let parts: Vec<&str> = intent_subject.split('.').collect();
assert!(
parts.len() >= 3 && parts[1] == "intents",
"Intent subject '{}' doesn't follow smith.intents.* pattern",
intent_subject
);
}
for result_subject in &result_subjects {
let parts: Vec<&str> = result_subject.split('.').collect();
assert!(
parts.len() >= 3 && parts[1] == "results",
"Result subject '{}' doesn't follow smith.results.* pattern",
result_subject
);
}
println!(
"✓ All {} subjects conform to Smith prefix contracts",
all_subjects.len()
);
println!("✓ Intent subjects: {}", intent_subjects.len());
println!("✓ Result subjects: {}", result_subjects.len());
println!("✓ Event subjects: {}", event_subjects.len());
println!("✓ Audit subjects: {}", audit_subjects.len());
}
#[test]
fn test_subject_builder_comprehensive() {
let subject = SubjectBuilder::new()
.part("test")
.capability("custom.v1")
.domain("service")
.build();
assert_eq!(subject, "smith.test.custom.v1.service");
let wildcard = SubjectBuilder::new().part("intents").wildcard();
assert_eq!(wildcard, "smith.intents.>");
let empty = SubjectBuilder::new().build();
assert_eq!(empty, "smith");
let complex = SubjectBuilder::new()
.part("complex")
.part("nested")
.capability("multi.level.v2")
.domain("advanced-service")
.build();
assert_eq!(
complex,
"smith.complex.nested.multi.level.v2.advanced-service"
);
}
#[test]
fn test_log_subject_builders() {
assert_eq!(builders::LogSubject::core("error"), "smith.logs.core.error");
assert_eq!(
builders::LogSubject::executor("debug"),
"smith.logs.executor.debug"
);
assert_eq!(
builders::LogSubject::admission("warn"),
"smith.logs.admission.warn"
);
assert_eq!(builders::LogSubject::http("info"), "smith.logs.http.info");
assert_eq!(
builders::LogSubject::service("custom-service", "trace"),
"smith.logs.custom-service.trace"
);
assert_eq!(
builders::LogSubject::error("test-service"),
"smith.logs.errors.test-service"
);
assert_eq!(
builders::LogSubject::performance("perf-service"),
"smith.logs.performance.perf-service"
);
assert_eq!(
builders::LogSubject::security("auth-service", "login-attempt"),
"smith.logs.security.auth-service.login-attempt"
);
assert_eq!(builders::LogSubject::all(), "smith.logs.>");
}
#[test]
fn test_event_subject_builders_comprehensive() {
assert_eq!(
builders::EventSubject::executor_lifecycle("exec-001"),
"smith.events.executor.lifecycle.exec-001"
);
assert_eq!(
builders::EventSubject::health("admission-service"),
"smith.events.health.admission-service"
);
assert_eq!(
builders::EventSubject::metrics("core-service"),
"smith.events.metrics.core-service"
);
}
#[test]
fn test_audit_subject_builders_comprehensive() {
assert_eq!(
builders::AuditSubject::execution("intent-abc-123"),
"smith.audit.execution.intent-abc-123"
);
assert_eq!(
builders::AuditSubject::policy("intent-policy-456"),
"smith.audit.policy.intent-policy-456"
);
assert_eq!(
builders::AuditSubject::security("failed-login"),
"smith.audit.security.failed-login"
);
}
#[test]
fn test_benchmark_subject_builders() {
assert_eq!(
builders::BenchmarkSubject::run("run-001"),
"smith.benchmark.runs.run-001"
);
assert_eq!(
builders::BenchmarkSubject::step("run-001", "step-001"),
"smith.benchmark.steps.run-001.step-001"
);
assert_eq!(
builders::BenchmarkSubject::tool_performance("run-001", "git"),
"smith.benchmark.tools.run-001.git"
);
assert_eq!(
builders::BenchmarkSubject::context_decision("run-001", "segment-001"),
"smith.benchmark.context.run-001.segment-001"
);
assert_eq!(
builders::BenchmarkSubject::failure("run-001", "timeout"),
"smith.benchmark.failures.run-001.timeout"
);
assert_eq!(
builders::BenchmarkSubject::optimizer_feedback("coding"),
"smith.benchmark.optimizer.coding"
);
assert_eq!(
builders::BenchmarkSubject::task_features("task-001"),
"smith.benchmark.task_features.task-001"
);
assert_eq!(
builders::BenchmarkSubject::early_stop("run-001", "resource-limit"),
"smith.benchmark.early_stop.run-001.resource-limit"
);
assert_eq!(builders::BenchmarkSubject::all(), "smith.benchmark.>");
}
#[test]
fn test_pattern_constants_comprehensive() {
assert_eq!(patterns::intents::RAW, "smith.intents.raw");
assert_eq!(patterns::intents::VETTED, "smith.intents.vetted");
assert_eq!(patterns::intents::QUARANTINE, "smith.intents.quarantine");
assert_eq!(patterns::intents::ALL, "smith.intents.>");
assert_eq!(patterns::intents::FS_READ_V1, "smith.intents.fs.read.v1");
assert_eq!(
patterns::intents::HTTP_FETCH_V1,
"smith.intents.http.fetch.v1"
);
assert_eq!(
patterns::intents::SHELL_EXEC_V1,
"smith.intents.shell.exec.v1"
);
assert_eq!(
patterns::intents::RAW_PLAYBOOK,
"smith.intents.raw.playbook"
);
assert_eq!(
patterns::intents::RAW_PLAYBOOK_ALL,
"smith.intents.raw.playbook.*"
);
assert_eq!(patterns::intents::RAW_MACRO, "smith.intents.raw.macro");
assert_eq!(
patterns::intents::RAW_MACRO_ALL,
"smith.intents.raw.macro.*"
);
assert_eq!(
patterns::intents::RAW_ATOM_USE,
"smith.intents.raw.atom.use"
);
assert_eq!(patterns::results::ALL, "smith.results.>");
assert_eq!(patterns::results::PREFIX, "smith.results");
assert_eq!(patterns::events::ALL, "smith.events.>");
assert_eq!(
patterns::events::EXECUTOR_LIFECYCLE,
"smith.events.executor.lifecycle"
);
assert_eq!(patterns::events::HEALTH, "smith.events.health");
assert_eq!(patterns::events::METRICS, "smith.events.metrics");
assert_eq!(patterns::audit::ALL, "smith.audit.>");
assert_eq!(patterns::audit::EXECUTION, "smith.audit.execution");
assert_eq!(patterns::audit::POLICY, "smith.audit.policy");
assert_eq!(patterns::audit::POLICY_ALL, "smith.audit.policy.*");
assert_eq!(patterns::audit::SECURITY, "smith.audit.security");
assert_eq!(patterns::ai_sdlc::ALL, "smith.ai.sdlc.>");
assert_eq!(
patterns::ai_sdlc::ACTION_REQUEST,
"smith.ai.sdlc.action.request"
);
assert_eq!(
patterns::ai_sdlc::ACTION_RESULT,
"smith.ai.sdlc.action.result"
);
assert_eq!(patterns::gitlab::ALL, "smith.gitlab.>");
assert_eq!(patterns::gitlab::EVENT_MR, "smith.gitlab.event.mr");
assert_eq!(
patterns::gitlab::EVENT_PIPELINE,
"smith.gitlab.event.pipeline"
);
}
#[test]
fn test_validation_functions_comprehensive() {
let code_with_violations = r#"
let subject1 = "smith.intents.raw.fs.read.v1";
let subject2 = "smith.results.intent-123";
let subject3 = "smith.events.health.check";
let subject4 = "smith.audit.execution.log";
"#;
let violations = validation::contains_raw_subject_patterns(code_with_violations);
assert!(!violations.is_empty(), "Should detect raw subject patterns");
assert!(validation::validate_centralized_usage("let x = 5;").is_ok());
assert!(validation::validate_centralized_usage(code_with_violations).is_err());
assert_eq!(
validation::extract_capability("smith.intents.shell.exec.v1"),
Some("shell.exec".to_string())
);
assert_eq!(
validation::extract_capability("smith.intents.complex.nested.multi.v2"),
Some("complex.nested".to_string())
);
assert_eq!(validation::extract_capability("invalid.subject"), None);
assert_eq!(validation::extract_capability("smith.results.123"), None);
assert_eq!(
validation::extract_intent_id("smith.results.complex-intent-id-with-dashes"),
Some("complex-intent-id-with-dashes".to_string())
);
assert_eq!(validation::extract_intent_id("invalid.subject"), None);
assert_eq!(
validation::extract_intent_id("smith.intents.fs.read.v1"),
None
);
assert!(validation::is_valid_smith_subject("smith.custom.subject"));
assert!(!validation::is_valid_smith_subject(""));
assert!(!validation::is_valid_smith_subject("nats.subject"));
assert!(!validation::is_valid_smith_subject("smith"));
assert!(validation::is_intent_subject("smith.intents.custom.action"));
assert!(!validation::is_intent_subject("smith.results.intent-123"));
assert!(!validation::is_intent_subject(""));
assert!(validation::is_result_subject("smith.results.any-id"));
assert!(!validation::is_result_subject("smith.intents.fs.read.v1"));
assert!(!validation::is_result_subject(""));
}
#[test]
fn test_stream_and_consumer_constants() {
assert_eq!(streams::INTENTS, "INTENTS");
assert_eq!(streams::RESULTS, "RESULTS");
assert_eq!(streams::EVENTS, "EVENTS");
assert_eq!(streams::AUDIT_LOGS, "AUDIT_LOGS");
assert_eq!(streams::BENCHMARK_RUNS, "BENCHMARK_RUNS");
assert_eq!(streams::BENCHMARK_STEPS, "BENCHMARK_STEPS");
assert_eq!(consumers::EXECUTOR, "executor");
assert_eq!(consumers::HTTP_EVENTS, "http-events");
assert_eq!(consumers::HTTP_RESULTS, "http-results");
assert_eq!(consumers::ADMISSION_RAW, "admission-raw");
assert_eq!(consumers::AUDIT_COLLECTOR, "audit-collector");
assert_eq!(consumers::BENCHMARK_COLLECTOR, "benchmark-collector");
assert_eq!(consumers::BENCHMARK_ANALYTICS, "benchmark-analytics");
assert_eq!(consumers::BENCHMARK_OPTIMIZER, "benchmark-optimizer");
assert_eq!(consumers::BENCHMARK_DASHBOARD, "benchmark-dashboard");
}
#[test]
fn test_abi_hash_generation() {
let hash1 = abi::generate_subject_abi_hash();
let hash2 = abi::generate_subject_abi_hash();
assert_eq!(hash1, hash2, "ABI hash should be deterministic");
assert!(!hash1.is_empty(), "ABI hash should not be empty");
assert!(
hash1.len() >= 32,
"ABI hash should be at least 32 characters"
);
assert!(abi::validate_subject_abi_stability(&hash1, &hash1).is_ok());
assert!(abi::validate_subject_abi_stability(&hash1, "different-hash").is_err());
}
#[test]
fn test_special_pattern_constants() {
assert_eq!(patterns::gitlab::EVENT_ISSUE, "smith.gitlab.event.issue");
assert_eq!(patterns::orchestrator::ALL, "smith.orchestrator.>");
assert_eq!(patterns::orchestrator::AUDIT, "smith.orchestrator.audit");
assert_eq!(patterns::orchestrator::STATUS, "smith.orchestrator.status");
assert_eq!(patterns::benchmark::RUNS, "smith.benchmark.runs");
assert_eq!(patterns::benchmark::STEPS, "smith.benchmark.steps");
assert_eq!(
patterns::benchmark::TOOL_PERFORMANCE,
"smith.benchmark.tools"
);
assert_eq!(
patterns::benchmark::CONTEXT_DECISIONS,
"smith.benchmark.context"
);
assert_eq!(
patterns::benchmark::FAILURE_ANALYSIS,
"smith.benchmark.failures"
);
assert_eq!(
patterns::benchmark::OPTIMIZER_FEEDBACK,
"smith.benchmark.optimizer"
);
assert_eq!(patterns::logs::ALL, "smith.logs.>");
assert_eq!(patterns::logs::CORE, "smith.logs.core");
assert_eq!(patterns::logs::CORE_ALL, "smith.logs.core.*");
assert_eq!(patterns::logs::EXECUTOR, "smith.logs.executor");
assert_eq!(patterns::logs::EXECUTOR_ALL, "smith.logs.executor.*");
assert_eq!(patterns::logs::ADMISSION, "smith.logs.admission");
assert_eq!(patterns::logs::ADMISSION_ALL, "smith.logs.admission.*");
assert_eq!(patterns::logs::HTTP, "smith.logs.http");
assert_eq!(patterns::logs::HTTP_ALL, "smith.logs.http.*");
assert_eq!(patterns::logs::ERRORS, "smith.logs.errors");
assert_eq!(patterns::logs::ERRORS_ALL, "smith.logs.errors.*");
assert_eq!(patterns::logs::PERFORMANCE, "smith.logs.performance");
assert_eq!(patterns::logs::PERFORMANCE_ALL, "smith.logs.performance.*");
assert_eq!(patterns::logs::SECURITY, "smith.logs.security");
assert_eq!(patterns::logs::SECURITY_ALL, "smith.logs.security.*");
}
#[test]
fn test_subject_builder_capability_variants() {
let simple_cap = SubjectBuilder::new().capability("fs").build();
assert_eq!(simple_cap, "smith.fs.v1");
let formatted_cap = SubjectBuilder::new().capability("fs.read.v1").build();
assert_eq!(formatted_cap, "smith.fs.read.v1");
let complex_cap = SubjectBuilder::new()
.part("test")
.capability("complex.nested.action")
.build();
assert_eq!(complex_cap, "smith.test.complex.nested.action");
}
#[test]
fn test_validation_error_handling() {
let clean_code = r#"
let subject = builders::IntentSubject::raw("fs.read.v1");
let result = builders::ResultSubject::for_intent("test");
"#;
let violations = validation::contains_raw_subject_patterns(clean_code);
assert!(
violations.is_empty(),
"Clean code should have no violations"
);
let validation_result = validation::validate_centralized_usage(clean_code);
assert!(
validation_result.is_ok(),
"Clean code should pass validation"
);
assert!(validation::validate_centralized_usage("").is_ok());
assert!(validation::contains_raw_subject_patterns("").is_empty());
}
#[test]
fn test_subject_extraction_edge_cases() {
assert_eq!(validation::extract_capability("smith.intents"), None);
assert_eq!(validation::extract_capability("smith.intents.fs"), None);
assert_eq!(validation::extract_capability("smith"), None);
assert_eq!(validation::extract_intent_id("smith.results"), None);
assert_eq!(validation::extract_intent_id("smith"), None);
assert_eq!(validation::extract_capability("other.system.intent"), None);
assert_eq!(validation::extract_intent_id("other.system.result"), None);
}
#[test]
fn test_additional_benchmark_patterns() {
assert_eq!(patterns::benchmark::ALL, "smith.benchmark.>");
assert_eq!(
patterns::benchmark::TASK_FEATURES,
"smith.benchmark.task_features"
);
assert_eq!(
patterns::benchmark::EARLY_STOPPING,
"smith.benchmark.early_stop"
);
assert_eq!(
patterns::benchmark::POLICY_ASSIGNMENTS,
"smith.benchmark.policy_assignments"
);
assert_eq!(
patterns::benchmark::REGRESSION_ALERTS,
"smith.benchmark.regressions"
);
}
#[test]
fn test_root_pattern_constant() {
assert_eq!(patterns::ROOT, "smith");
let builder_subject = SubjectBuilder::new().build();
assert!(builder_subject.starts_with(patterns::ROOT));
assert_eq!(builder_subject, patterns::ROOT);
}
#[test]
fn test_abi_version_constant() {
assert_eq!(abi::SUBJECT_ABI_VERSION, 1);
let hash = abi::generate_subject_abi_hash();
assert!(hash.len() == 64); }
#[test]
fn test_comprehensive_constant_coverage() {
let benchmark_patterns = vec![
patterns::benchmark::ALL,
patterns::benchmark::RUNS,
patterns::benchmark::STEPS,
patterns::benchmark::TOOL_PERFORMANCE,
patterns::benchmark::CONTEXT_DECISIONS,
patterns::benchmark::FAILURE_ANALYSIS,
patterns::benchmark::OPTIMIZER_FEEDBACK,
patterns::benchmark::TASK_FEATURES,
patterns::benchmark::EARLY_STOPPING,
patterns::benchmark::POLICY_ASSIGNMENTS,
patterns::benchmark::REGRESSION_ALERTS,
];
for pattern in benchmark_patterns {
assert!(pattern.starts_with("smith.benchmark"));
assert!(!pattern.is_empty());
}
let stream_names = vec![
streams::INTENTS_RAW,
streams::INTENTS_VETTED,
streams::INTENTS_QUARANTINE,
streams::SYSTEM_EVENTS,
streams::BENCHMARK_CONTEXT,
streams::BENCHMARK_FAILURES,
streams::BENCHMARK_OPTIMIZER,
];
for stream_name in stream_names {
assert!(!stream_name.is_empty());
assert!(stream_name
.chars()
.all(|c| c.is_ascii_uppercase() || c == '_'));
}
let consumer_names = vec![consumers::EXECUTOR_FS_READ, consumers::EXECUTOR_HTTP_FETCH];
for consumer_name in consumer_names {
assert!(!consumer_name.is_empty());
assert!(consumer_name.starts_with("executor"));
}
}
}