async fn ttt_sessions_stats(State(state): State<AppState>) -> impl IntoResponse {
Json(serde_json::json!({
"active_sessions": state.ttt_sessions.len(),
"compression_active": state.compression_active(),
"openai_compression_active": state.openai_compression_active(),
"threshold_tokens": state.compressor_config.heavy_message_threshold_tokens,
"recall_top_k": state.compressor_config.recall_top_k,
}))
}
async fn ttt_session_drop(
State(state): State<AppState>,
Path(id): Path<String>,
) -> impl IntoResponse {
let removed = match state.ttt_sessions.take_session(&id) {
Some(handle) => {
state.flush_session_to_vibe(&handle).await;
true
}
None => false,
};
emit_savings_receipt(&state, &id);
Json(serde_json::json!({ "session_id": id, "removed": removed }))
}
async fn ttt_sessions_clear(State(state): State<AppState>) -> impl IntoResponse {
state.flush_all_sessions_to_vibe().await;
state.ttt_sessions.clear();
Json(serde_json::json!({ "cleared": true }))
}
async fn ttt_feedback(
State(state): State<AppState>,
Json(req): Json<TttFeedbackRequest>,
) -> Result<Json<TttFeedbackResponse>, ApiError> {
let response = state
.adapt_feedback_to_cache(req, &compression_cache_path())
.await?;
Ok(Json(response))
}
async fn hypervisor_mount(
State(state): State<AppState>,
Json(req): Json<HypervisorMountRequest>,
) -> Result<Json<HypervisorMountResponse>, ApiError> {
if req.root.trim().is_empty() {
return Err(ApiError::BadRequest("root is required".into()));
}
let mount = state
.neural_vfs
.mount(req.root.trim())
.map_err(ApiError::BadRequest)?;
state
.swarm_matrix
.observe_vfs_target(&mount.mounted_root)
.await;
let session_id = req
.session_id
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or("hypervisor-vfs");
let mut warmed = Vec::new();
for path in req.warm_paths {
let report = state
.neural_vfs
.read_file_and_prefill(
path,
session_id,
state.pipeline.clone(),
state.ttt_sessions.clone(),
)
.await
.map_err(ApiError::BadRequest)?;
warmed.push(report);
}
Ok(Json(HypervisorMountResponse {
mount,
warmed,
vfs: state.neural_vfs.status(),
}))
}
async fn hypervisor_read(
State(state): State<AppState>,
Json(req): Json<HypervisorReadRequest>,
) -> Result<Json<HypervisorReadResponse>, ApiError> {
if req.path.trim().is_empty() {
return Err(ApiError::BadRequest("path is required".into()));
}
let session_id = req
.session_id
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or("hypervisor-vfs");
let read = state
.neural_vfs
.read_file_and_prefill(
req.path.trim(),
session_id,
state.pipeline.clone(),
state.ttt_sessions.clone(),
)
.await
.map_err(ApiError::BadRequest)?;
Ok(Json(HypervisorReadResponse {
read,
vfs: state.neural_vfs.status(),
}))
}
async fn hypervisor_list(
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
let path = body.get("path").and_then(Value::as_str).unwrap_or(".");
let entries = state
.neural_vfs
.readdir(path)
.map_err(ApiError::BadRequest)?;
Ok(Json(serde_json::json!({
"entries": entries,
"vfs": state.neural_vfs.status(),
})))
}
async fn hypervisor_stat(
State(state): State<AppState>,
Json(body): Json<Value>,
) -> Result<Json<Value>, ApiError> {
let Some(path) = body.get("path").and_then(Value::as_str) else {
return Err(ApiError::BadRequest("path is required".into()));
};
let attr = state
.neural_vfs
.getattr(path)
.map_err(ApiError::BadRequest)?;
Ok(Json(serde_json::json!({
"attr": attr,
"vfs": state.neural_vfs.status(),
})))
}
async fn hypervisor_jit_run(
State(state): State<AppState>,
Json(req): Json<HypervisorJitRunRequest>,
) -> Result<Json<HypervisorJitRunResponse>, ApiError> {
if !state.jit_exec_enabled {
return Err(ApiError::Forbidden(
"process execution is disabled on this server; set AXIOM_ENABLE_JIT_EXEC=1 \
to enable the poly-jit repair capability (arbitrary command execution) — \
see docs/SECURITY-AUDIT.md before enabling on a network-reachable host"
.into(),
));
}
if req.command.trim().is_empty() {
return Err(ApiError::BadRequest("command is required".into()));
}
let session_id = req
.session_id
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or("hypervisor-jit")
.to_string();
let source_path = req.source_path.clone();
let backup = match source_path.as_deref() {
Some(p) => Some(
tokio::fs::read_to_string(p)
.await
.map_err(|e| ApiError::BadRequest(format!("cannot read source_path: {e}")))?,
),
None => None,
};
let run_req = PolyJitRunRequest {
session_id: session_id.clone(),
command: req.command,
args: req.args,
working_dir: req.working_dir,
source_path: source_path.clone(),
};
let cache_path = compression_cache_path();
let report = state
.poly_jit
.run_with_feedback(run_req, |diag| {
let state = state.clone();
let cache_path = cache_path.clone();
async move {
let message = format!(
"poly-jit fault at step {} (exit {:?})",
diag.step, diag.status_code
);
let trace = format!("{}\n{}", diag.stdout, diag.stderr);
if let Err(e) = state
.adapt_feedback_to_cache(
TttFeedbackRequest {
session_id: diag.session_id.clone(),
message,
feedback_type: Some("poly_jit_fault".to_string()),
trace: Some(trace),
},
&cache_path,
)
.await
{
eprintln!("[poly-jit] TTT feedback skipped: {e:?}");
}
Ok(())
}
})
.await
.map_err(|e| ApiError::Internal(format!("poly-jit run failed: {e}")))?;
let mut source_restored = false;
if !report.passed {
if let (Some(p), Some(b)) = (source_path.as_deref(), backup.as_ref()) {
tokio::fs::write(p, b).await.map_err(|e| {
eprintln!("[poly-jit] failed to restore source_path '{p}': {e}");
ApiError::Internal(format!("failed to restore source_path '{p}': {e}"))
})?;
source_restored = true;
}
}
Ok(Json(HypervisorJitRunResponse {
report,
source_restored,
jit: state.poly_jit.status(),
}))
}
async fn hypervisor_jit_status(State(state): State<AppState>) -> Json<HypervisorJitStatusResponse> {
Json(HypervisorJitStatusResponse {
jit: state.poly_jit.status(),
vfs: state.neural_vfs.status(),
})
}
async fn hypervisor_quantum_coherent_state(
State(state): State<AppState>,
) -> Json<HypervisorQuantumStateResponse> {
Json(HypervisorQuantumStateResponse {
quantum: state.poly_jit.quantum_status(),
})
}
async fn swarm_matrix_state(State(state): State<AppState>) -> Json<SwarmMatrixStateResponse> {
let dwe = state.dwe_bus.telemetry();
let exact_residual = state.exact_residual_cache.telemetry();
let matrix = state
.swarm_matrix
.state(dwe.clone(), exact_residual.clone());
Json(SwarmMatrixStateResponse {
matrix,
dwe,
exact_residual,
})
}