From 403e880830390e945ae7cc3f28a76b34bd8ceb1f Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Sat, 12 Sep 2026 10:37:49 +0200 Subject: [PATCH] feat(agent): push usage events to the registry (scan.completed, target.added) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PRODUCT_INTEGRATION_SPEC 2.0 §9.2 / M-MIG-7: every completed pipeline or DAST scan and every added target becomes an idempotent usage event on POST /v1/usage/events (registry forwards it to ERPNext). Background, best-effort, no-op without a registry. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- compliance-agent/src/agent.rs | 44 ++++++++++++++++--- compliance-agent/src/api/handlers/dast.rs | 22 +++++++++- .../src/api/handlers/onboarding.rs | 7 +++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/compliance-agent/src/agent.rs b/compliance-agent/src/agent.rs index a953a17..40df275 100644 --- a/compliance-agent/src/agent.rs +++ b/compliance-agent/src/agent.rs @@ -66,10 +66,36 @@ impl ComplianceAgent { repo_id: &str, trigger: compliance_core::models::ScanTrigger, ) -> Result<(), crate::error::AgentError> { - let db = self.db_pool.for_tenant_id(tenant_id).await?; - let orchestrator = - PipelineOrchestrator::new(self.config.clone(), db, self.llm.clone(), self.http.clone()); - orchestrator.run_target(repo_id, trigger).await + self.run_target_scan(tenant_id, repo_id, trigger).await + } + + /// Push one usage event to the tenant-registry (PRODUCT_INTEGRATION_SPEC + /// 2.0 §9.2) in the background. `key` makes retries idempotent; the + /// registry forwards the event to ERPNext. No-op without a registry. + pub fn emit_usage( + &self, + tenant_id: &str, + metric: &str, + key: String, + properties: serde_json::Value, + ) { + let Some(registry) = self.registry.clone() else { + return; + }; + let event = crate::registry::UsageEvent { + idempotency_key: key, + tenant_id: tenant_id.to_string(), + product: registry.product().to_string(), + metric: metric.to_string(), + quantity: 1.0, + occurred_at: chrono::Utc::now().to_rfc3339(), + properties: Some(properties), + }; + tokio::spawn(async move { + if let Err(e) = registry.push_usage_events(&[event]).await { + tracing::warn!("usage event push failed: {e}"); + } + }); } /// Alias for [`Self::run_scan`] — every scan runs the unified onboarded-target @@ -84,7 +110,15 @@ impl ComplianceAgent { let db = self.db_pool.for_tenant_id(tenant_id).await?; let orchestrator = PipelineOrchestrator::new(self.config.clone(), db, self.llm.clone(), self.http.clone()); - orchestrator.run_target(target_id, trigger).await + let started = chrono::Utc::now(); + orchestrator.run_target(target_id, trigger.clone()).await?; + self.emit_usage( + tenant_id, + "scan.completed", + format!("scan:{tenant_id}:{target_id}:{}", started.timestamp()), + serde_json::json!({ "target_id": target_id, "trigger": format!("{trigger:?}") }), + ); + Ok(()) } /// Run a PR review: scan the diff and post review comments. diff --git a/compliance-agent/src/api/handlers/dast.rs b/compliance-agent/src/api/handlers/dast.rs index ecd9fff..e1f2f26 100644 --- a/compliance-agent/src/api/handlers/dast.rs +++ b/compliance-agent/src/api/handlers/dast.rs @@ -95,10 +95,22 @@ pub async fn add_target( target.allow_destructive = req.allow_destructive; let db = tenant_db(&agent, &tenant).await?; - db.dast_targets() + let res = db + .dast_targets() .insert_one(&target) .await .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let target_id = res + .inserted_id + .as_object_id() + .map(|id| id.to_hex()) + .unwrap_or_default(); + agent.emit_usage( + &tenant.0.tenant_id, + "target.added", + format!("dast-target:{}:{target_id}", tenant.0.tenant_id), + serde_json::json!({ "target_id": target_id, "kind": "dast" }), + ); Ok(Json(ApiResponse { data: target, @@ -124,6 +136,8 @@ pub async fn trigger_scan( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .ok_or(StatusCode::NOT_FOUND)?; + let tenant_id = tenant.0.tenant_id.clone(); + let usage_agent = agent.clone(); tokio::spawn(async move { let orchestrator = compliance_dast::DastOrchestrator::new(100); match orchestrator.run_scan(&target, Vec::new()).await { @@ -131,6 +145,12 @@ pub async fn trigger_scan( if let Err(e) = db.dast_scan_runs().insert_one(&scan_run).await { tracing::error!("Failed to store DAST scan run: {e}"); } + usage_agent.emit_usage( + &tenant_id, + "scan.completed", + format!("dast:{tenant_id}:{id}:{}", chrono::Utc::now().timestamp()), + serde_json::json!({ "target_id": id, "kind": "dast", "findings": findings.len() }), + ); for finding in &findings { if let Err(e) = db.dast_findings().insert_one(finding).await { tracing::error!("Failed to store DAST finding: {e}"); diff --git a/compliance-agent/src/api/handlers/onboarding.rs b/compliance-agent/src/api/handlers/onboarding.rs index b51efd3..e9bd6e0 100644 --- a/compliance-agent/src/api/handlers/onboarding.rs +++ b/compliance-agent/src/api/handlers/onboarding.rs @@ -152,6 +152,13 @@ pub async fn create_target( .await .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; target.id = res.inserted_id.as_object_id(); + let target_id = target.id.map(|id| id.to_hex()).unwrap_or_default(); + agent.emit_usage( + &tenant.0.tenant_id, + "target.added", + format!("target:{}:{target_id}", tenant.0.tenant_id), + serde_json::json!({ "target_id": target_id, "target_type": format!("{:?}", target.target_type) }), + ); Ok(Json(ApiResponse { data: target, total: None, -- 2.54.1