use crate::jsonrpc::{Error, Result};
use crate::{lock_mesh, lock_registry, update_diagnostics};
use lsp_types_max::DiagnosticSeverity;
use serde_json::Value;
pub async fn max_clear_diagnostic(params: String) -> Result<()> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
registry.cleared_diagnostics.insert(params.clone());
if registry.diagnostics.remove(¶ms).is_some() {
registry.repair_plans.remove(¶ms);
Ok(())
} else {
Err(Error::invalid_params(format!(
"Diagnostic '{}' not found",
params
)))
}
}
pub async fn max_receipt(params: String) -> Result<max_protocol::Receipt> {
let registry = lock_registry()?;
if let Some(rcpt) = registry.receipts.get(¶ms) {
Ok(rcpt.clone())
} else {
Err(Error::invalid_params(format!(
"Receipt '{}' not found",
params
)))
}
}
pub async fn max_release_actuation(params: Value) -> Result<Value> {
let instance_id = params
.get("instance_id")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or_else(|| Error::invalid_params("missing instance_id"))?;
let mut registry = lock_registry()?;
let instance_diag_count = registry
.diagnostics
.values()
.filter(|d| d.diagnostic_id.contains(&instance_id))
.count();
if instance_diag_count > 0 {
return Err(Error::request_failed(format!(
"Release refused: {} active diagnostics blocking conformance",
instance_diag_count
)));
}
let mut assembler = affidavit::chain::ChainAssembler::new();
let event = affidavit::types::OperationEvent {
id: format!("evt-release-{}", instance_id),
seq: registry.action_seq,
event_type: "release_actuation".to_string(),
objects: vec![],
payload_commitment: affidavit::types::Blake3Hash::from_bytes(
b"release approved by autonomic loop",
),
};
assembler
.append(event)
.map_err(|_| Error::internal_error())?;
let affidavit_receipt = assembler.finalize();
let receipt_id = format!("rcpt-release-{}", instance_id);
let receipt = max_protocol::Receipt {
receipt_id: receipt_id.clone(),
hash: affidavit_receipt.chain_hash.0,
prev_receipt_hash: None,
};
registry
.receipts
.insert(receipt_id.clone(), receipt.clone());
registry.action_seq = registry.action_seq.saturating_add(1);
let seq = registry.action_seq;
registry
.conformance_delta_log
.push_back(crate::max_runtime::ConformanceDeltaEntry {
seq,
instance_id: instance_id.clone(),
old_score: 100.0,
new_score: 100.0,
timestamp: crate::rfc3339_now(),
});
const MAX_DELTA_LOG: usize = 4096;
if registry.conformance_delta_log.len() > MAX_DELTA_LOG {
registry.conformance_delta_log.pop_front();
}
Ok(serde_json::json!({
"released": true,
"instance_id": instance_id,
"conformance_score": 100.0,
"release_receipt": receipt,
}))
}
pub async fn max_admission() -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
let verdict = if registry.diagnostics.is_empty() {
"Admitted"
} else if registry
.diagnostics
.values()
.any(|d| matches!(d.lsp.severity, Some(DiagnosticSeverity::ERROR)))
{
"Refused"
} else {
"Unknown"
};
Ok(serde_json::json!({
"verdict": verdict,
"diagnostic_count": registry.diagnostics.len(),
}))
}
pub async fn max_autonomic_loop() -> Result<serde_json::Value> {
let registry = lock_registry()?;
Ok(serde_json::json!({
"snapshot_count": registry.snapshots.len(),
"diagnostic_count": registry.diagnostics.len(),
"receipt_count": registry.receipts.len(),
"gate_count": registry.gates.len(),
}))
}
pub async fn max_chain() -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
let diagnostics: Vec<serde_json::Value> = registry
.diagnostics
.values()
.map(|d| {
serde_json::json!({
"id": d.diagnostic_id,
"law_id": d.law_id,
"severity": format!("{:?}", d.lsp.severity),
"message": d.lsp.message,
})
})
.collect();
let receipts: Vec<serde_json::Value> = registry
.receipts
.values()
.map(|r| {
serde_json::json!({
"receipt_id": r.receipt_id,
"hash": r.hash,
})
})
.collect();
Ok(serde_json::json!({
"diagnostic_count": diagnostics.len(),
"receipt_count": receipts.len(),
"diagnostics": diagnostics,
"receipts": receipts,
}))
}
pub async fn max_hook() -> Result<serde_json::Value> {
let mesh = lock_mesh()?;
let descriptors = mesh.hook_descriptors();
serde_json::to_value(&descriptors).map_err(|_| crate::jsonrpc::Error::internal_error())
}
pub async fn max_hook_graph() -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
let mesh = lock_mesh()?;
let descriptors = mesh.hook_descriptors();
let active_count = descriptors.len();
let hooks: Vec<_> = descriptors
.iter()
.map(|d| {
serde_json::json!({
"hook": d.name,
"trigger_law": d.trigger_law,
"input_type": d.input_type,
"output_type": d.output_type,
"failure_mode": d.failure_mode,
})
})
.collect();
Ok(serde_json::json!({
"active_hook_count": active_count,
"active_diagnostic_count": registry.diagnostics.len(),
"active_receipt_count": registry.receipts.len(),
"hooks": hooks,
}))
}
pub async fn max_lawful_transition(params: String) -> Result<serde_json::Value> {
let registry = lock_registry()?;
let current = registry.current_state;
let phase_order = [
"Uninitialized",
"Initializing",
"Initialized",
"ShutDown",
"Exited",
];
let current_str = format!("{:?}", current);
let current_idx = phase_order.iter().position(|&p| p == current_str.as_str());
let target_idx = phase_order.iter().position(|&p| p == params.as_str());
let (admitted, refused_reason) = match (current_idx, target_idx) {
(Some(ci), Some(ti)) if ti == ci + 1 => {
let blocking_count = registry
.diagnostics
.values()
.filter(|d| matches!(d.lsp.severity, Some(DiagnosticSeverity::ERROR)))
.count();
if blocking_count == 0 {
(true, serde_json::Value::Null)
} else {
(
false,
serde_json::json!(format!("Blocked by {} error diagnostic(s)", blocking_count)),
)
}
}
(Some(ci), Some(ti)) if ti <= ci => (
false,
serde_json::json!(format!("Backward transitions are not lawful")),
),
_ => (
false,
serde_json::json!(format!(
"Unknown phase(s): current='{:?}', target='{}'",
current, params
)),
),
};
Ok(serde_json::json!({
"current_phase": format!("{:?}", current),
"requested_phase": params,
"admitted": admitted,
"refused_reason": refused_reason,
}))
}
pub async fn max_ledger_report() -> Result<String> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
let mut report = "Ledger Diagnostic Report for Instance: LSP_1\n".to_string();
report.push_str("Status: VERIFIED (Cryptographic integrity intact)\n");
report.push_str(&format!("Active Phase: {:?}\n", registry.current_state));
report.push_str(&format!("Receipts count: {}\n", registry.receipts.len()));
let mut sorted_receipts: Vec<_> = registry.receipts.values().cloned().collect();
sorted_receipts.sort_by_key(|r| r.receipt_id.clone());
for (idx, r) in sorted_receipts.iter().enumerate() {
report.push_str(&format!(
" [{}] ID: {} | Hash: {}\n",
idx, r.receipt_id, r.hash
));
}
report.push_str(&format!(
"Ledger Report — {} diagnostic(s)\n",
registry.diagnostics.len()
));
for (id, diag) in ®istry.diagnostics {
report.push_str(&format!(
" [{}] severity={:?} law={} msg={}\n",
id, diag.lsp.severity, diag.law_id, diag.lsp.message
));
}
Ok(report)
}
pub async fn max_manifold_snapshot() -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
update_diagnostics(&mut registry);
Ok(serde_json::json!({
"snapshot_count": registry.snapshots.len(),
"diagnostic_count": registry.diagnostics.len(),
"receipt_count": registry.receipts.len(),
"gate_count": registry.gates.len(),
"current_state": format!("{:?}", registry.current_state),
}))
}
pub async fn max_propagate(params: max_protocol::Receipt) -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
let receipt_id = params.receipt_id.clone();
registry.receipts.insert(receipt_id.clone(), params);
Ok(serde_json::json!({ "propagated": true, "receipt_id": receipt_id }))
}
pub async fn max_refusal(params: String) -> Result<serde_json::Value> {
let mut registry = lock_registry()?;
let receipt_id = format!("rcpt-refusal-{}", params);
let hash = crate::sha256(receipt_id.as_bytes());
let receipt = max_protocol::Receipt {
receipt_id: receipt_id.clone(),
hash,
prev_receipt_hash: None,
};
registry.receipts.insert(receipt_id, receipt.clone());
Ok(serde_json::json!({
"refused": true,
"diagnostic_id": params,
"receipt": receipt,
}))
}
pub async fn max_replay() -> Result<serde_json::Value> {
let registry = lock_registry()?;
let receipts: Vec<serde_json::Value> = registry
.receipts
.values()
.map(|r| serde_json::json!({ "receipt_id": r.receipt_id, "hash": r.hash }))
.collect();
Ok(serde_json::json!({
"receipt_count": receipts.len(),
"receipts": receipts,
}))
}
pub async fn max_verify_ledger() -> Result<serde_json::Value> {
let registry = lock_registry()?;
let mut errors: Vec<String> = Vec::new();
for (id, rcpt) in ®istry.receipts {
let expected = crate::sha256(rcpt.receipt_id.as_bytes());
if !rcpt.receipt_id.contains(':') && rcpt.hash != expected {
errors.push(format!("Receipt '{}' hash mismatch", id));
}
}
Ok(serde_json::json!({
"valid": errors.is_empty(),
"receipt_count": registry.receipts.len(),
"errors": errors,
}))
}