From 1cca509d4ddd623810edda6116c7e6219751166e Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 5 Oct 2026 22:00:27 +0200 Subject: [PATCH] fix(git-source): managed targets get the type their code shows Every target the managed git sync created stayed "Backend / API", the Android+Unity repository and the firmware repositories included. The sync creates a target when its repository is created, before anything is pushed, so detection then saw no code and fell back to the default type; and the classification each scan computes was stored, but never applied to the target's type. A scan now applies it to a managed target (OnboardedTarget::detected_type) when the classifier found evidence (not only the fallback), the evidence names another type, and no person has chosen the type. The default compliance profile of the new type comes with it. Changing the type in the editor sets `type_chosen`, and the scans leave it alone from then on; the editor sends the type on every save, so only a change counts. Existing managed targets are re-typed by their next scan. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- .../src/api/handlers/onboarding.rs | 35 ++++++---- compliance-agent/src/pipeline/orchestrator.rs | 24 +++++-- compliance-core/src/models/onboarding.rs | 69 +++++++++++++++++++ 3 files changed, 111 insertions(+), 17 deletions(-) diff --git a/compliance-agent/src/api/handlers/onboarding.rs b/compliance-agent/src/api/handlers/onboarding.rs index 06a6159..148b7d7 100644 --- a/compliance-agent/src/api/handlers/onboarding.rs +++ b/compliance-agent/src/api/handlers/onboarding.rs @@ -315,25 +315,34 @@ pub async fn update_target( if let Some(name) = req.name { set.insert("name", name); } + // Both merge with what is stored, so the stored target is read once. + let stored = if req.scan_config.is_some() + || req.artifacts.is_some() + || req.project_ids.is_some() + || req.target_type.is_some() + { + Some( + db.onboarded_targets() + .find_one(doc! { "_id": oid }) + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? + .ok_or(StatusCode::NOT_FOUND)?, + ) + } else { + None + }; if let Some(tt) = req.target_type { set.insert( "target_type", to_bson(&tt).map_err(|_| StatusCode::BAD_REQUEST)?, ); + // A person picked another type: the scans' detection no longer + // changes it (the editor sends the type on every save, so only a + // change counts). + if stored.as_ref().is_some_and(|s| s.target_type != tt) { + set.insert("type_chosen", true); + } } - // Both merge with what is stored, so the stored target is read once. - let stored = - if req.scan_config.is_some() || req.artifacts.is_some() || req.project_ids.is_some() { - Some( - db.onboarded_targets() - .find_one(doc! { "_id": oid }) - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? - .ok_or(StatusCode::NOT_FOUND)?, - ) - } else { - None - }; let credential_error = |e: SecretError| { tracing::error!("storing a credential: {e}"); StatusCode::SERVICE_UNAVAILABLE diff --git a/compliance-agent/src/pipeline/orchestrator.rs b/compliance-agent/src/pipeline/orchestrator.rs index 066027c..e2df54e 100644 --- a/compliance-agent/src/pipeline/orchestrator.rs +++ b/compliance-agent/src/pipeline/orchestrator.rs @@ -1414,13 +1414,29 @@ impl PipelineOrchestrator { crate::pipeline::firmware_sbom::is_firmware_target(classification.suggested); if let (Some(oid), Ok(bson)) = (target.id, mongodb::bson::to_bson(&classification)) { + let mut set = doc! { "classification": bson }; + // A managed target was created before its repository had + // code, so with a default type: the code decides it now. + if let Some(detected) = target.detected_type(&classification) { + let profile = compliance_core::models::default_compliance_profile(detected); + if let (Ok(tt), Ok(profile)) = ( + mongodb::bson::to_bson(&detected), + mongodb::bson::to_bson(&profile), + ) { + tracing::info!( + target_id, + from = %target.target_type, + to = %detected, + "managed target typed by its code" + ); + set.insert("target_type", tt); + set.insert("compliance_profile", profile); + } + } let _ = self .db .onboarded_targets() - .update_one( - doc! { "_id": oid }, - doc! { "$set": { "classification": bson } }, - ) + .update_one(doc! { "_id": oid }, doc! { "$set": set }) .await; } } diff --git a/compliance-core/src/models/onboarding.rs b/compliance-core/src/models/onboarding.rs index d825833..e720972 100644 --- a/compliance-core/src/models/onboarding.rs +++ b/compliance-core/src/models/onboarding.rs @@ -529,6 +529,21 @@ pub struct ComplianceProfile { /// The sensible default compliance scope for a target type. Firmware / PLC / /// embedded default to CRA + IEC 62443; software defaults to GDPR + SOC 2. +impl OnboardedTarget { + /// The type a scan's `classification` gives a managed target, when it + /// should change: the target came from a managed git source (created + /// before its repository had any code, so typed by default), no person + /// chose its type, the classifier found evidence (not just the fallback + /// to the current type), and the evidence names another type. + pub fn detected_type(&self, classification: &Classification) -> Option { + let detected = self.source_repo.is_some() + && !self.type_chosen + && !classification.candidates.is_empty() + && classification.suggested != self.target_type; + detected.then_some(classification.suggested) + } +} + pub fn default_compliance_profile(target_type: TargetType) -> ComplianceProfile { use ComplianceFramework::{Cra, Gdpr, Iec62443, Soc2}; let frameworks = match target_type { @@ -598,6 +613,10 @@ pub struct OnboardedTarget { /// for a target a user onboarded. #[serde(default, skip_serializing_if = "Option::is_none")] pub source_repo: Option, + /// A person changed the target's type. A managed target's type otherwise + /// follows what each scan detects (see [`OnboardedTarget::detected_type`]). + #[serde(default)] + pub type_chosen: bool, /// When the target was archived because its repository was deleted. /// Archived targets keep their findings but are no longer scanned. #[serde( @@ -642,6 +661,7 @@ impl OnboardedTarget { findings_count: 0, project_ids: Vec::new(), source_repo: None, + type_chosen: false, archived_at: None, created_at: now, updated_at: now, @@ -676,6 +696,55 @@ impl OnboardedTarget { mod tests { use super::*; + fn classified(suggested: TargetType, evidence: bool) -> Classification { + Classification { + suggested, + candidates: if evidence { + vec![TargetTypeCandidate { + target_type: suggested, + confidence: 0.8, + rationale: "Android manifest / gradle".into(), + }] + } else { + Vec::new() + }, + facts: Vec::new(), + detected_by: Vec::new(), + detected_at: Utc::now(), + confirmed: false, + } + } + + #[test] + fn a_managed_targets_type_follows_its_code_until_a_person_picks_one() { + let mut managed = OnboardedTarget::new("gt-mobile".into(), TargetType::BackendService); + managed.source_repo = Some(super::super::git_source::SourceRepo { + base_url: "https://kunde.code.example".into(), + repo_id: 7, + full_name: "kunde/gt-mobile".into(), + }); + let android = classified(TargetType::AndroidApp, true); + assert_eq!( + managed.detected_type(&android), + Some(TargetType::AndroidApp) + ); + // An empty repository gives no evidence: the default stays. + assert_eq!( + managed.detected_type(&classified(TargetType::BackendService, false)), + None + ); + // Already right: nothing to change. + managed.target_type = TargetType::AndroidApp; + assert_eq!(managed.detected_type(&android), None); + // A person's choice stands. + managed.target_type = TargetType::DesktopApp; + managed.type_chosen = true; + assert_eq!(managed.detected_type(&android), None); + // A target a person onboarded keeps the type they gave it. + let onboarded = OnboardedTarget::new("fw".into(), TargetType::BackendService); + assert_eq!(onboarded.detected_type(&android), None); + } + fn sample_target() -> OnboardedTarget { let mut t = OnboardedTarget::new("acme-web".to_string(), TargetType::WebApp); t.artifacts.push(Artifact::git_repo( -- 2.54.1