Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ed9b8cc
AST-195928 - Add Dev Assist analytics fields to agent-hook telemetry
cx-rahul-pidde Nov 26, 2026
9cbe21f
AST-195928 - Complete Dev Assist scan and ignore telemetry
cx-rahul-pidde Nov 27, 2026
453c151
AST-195928 - Fix hook telemetry accuracy and latency
cx-rahul-pidde Nov 27, 2026
650f3f2
AST-195928 - Complete CLI authentication and scanner telemetry
cx-rahul-pidde Nov 27, 2026
8da49a8
AST-195928 - Report Dev Assist CLI operation failures
cx-rahul-pidde Nov 30, 2026
2a799a9
AST-195928 Normalize OSS remediation severity
cx-rahul-pidde Nov 30, 2026
ef14f03
AST-195928 Emit verification telemetry for realtime hook fixes
cx-rahul-pidde Nov 30, 2026
1258b7f
AST-195928 Use skill-driven verification telemetry
cx-rahul-pidde Nov 30, 2026
37b409c
AST-195928 Attach login metadata and confirm verification publish
cx-rahul-pidde Nov 30, 2026
336be64
AST-195928 Verify realtime fixes through a hidden scan flag
cx-rahul-pidde Nov 30, 2026
8dc1a05
AST-195928 Remove realtime fix verification telemetry
cx-rahul-pidde Nov 30, 2026
1d97547
AST-195928 Report CLI agent telemetry fields from the design
cx-rahul-pidde Nov 30, 2026
8fc4fc7
AST-195928 Authenticate telemetry for failed re-logins
cx-rahul-pidde Dec 1, 2026
1bfe563
AST-195928 Keep failed logins cx cannot send for the Dev Assist plugin
cx-rahul-pidde Dec 1, 2026
325a07e
AST-195928 Address review findings on the agent telemetry changes
cx-rahul-pidde Dec 2, 2026
9c6aa69
AST-195928 Fix golangci-lint findings in the agent telemetry changes
cx-rahul-pidde Dec 2, 2026
730111f
AST-195928 Remove the empty config.yaml a test run left behind
cx-rahul-pidde Dec 2, 2026
84dcfcf
AST-195928 Fix the remaining lint findings and restore LF line endings
cx-rahul-pidde Dec 2, 2026
20c23d5
AST-195928 Name the authentication failure reasons
cx-rahul-pidde Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions internal/commands/agenthooks/cx/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import (

agenthooks "github.com/Checkmarx/ast-cx-hooks"
"github.com/Checkmarx/ast-cx-hooks/claude"
"github.com/Checkmarx/ast-cx-hooks/codex"
"github.com/Checkmarx/ast-cx-hooks/copilot"
"github.com/Checkmarx/ast-cx-hooks/copilotcli"
"github.com/Checkmarx/ast-cx-hooks/cursor"
"github.com/Checkmarx/ast-cx-hooks/gemini"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/asca"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/kics"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/secrets"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/hooktelemetry"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/hostenv"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/sca"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/scannerconfig"
Expand All @@ -20,15 +25,17 @@ import (
"github.com/checkmarx/ast-cli/internal/wrappers"
)

// canonical per-engine labels for the session summary (normalizes the legacy SCA/Oss split used by
// the per-event remediation telemetry; ASCA already uses "Asca").
// Canonical per-engine labels for the session summary.
const (
engineAsca = "Asca"
engineSca = "Sca"
engineKics = "Kics"
engineSecrets = "Secrets"
)

// remediationInstructionsMissing is the reason a remediation event reports when the hook had no guidance to give.
const remediationInstructionsMissing = "remediation_instructions_missing"

// scaScanner is the package-level SCA scanner used by the guardrail handlers.
// It is set by RegisterGuardrails so the handlers (free functions registered
// with the agenthooks library) can reach it without an injection mechanism.
Expand Down Expand Up @@ -70,6 +77,27 @@ func sessionIDFromToolCall(ev *agenthooks.ToolCallEvent) string {
return ""
}

// telemetrySessionIDFromToolCall is the agent's session id for a ToolCall event, from every agent's raw
// payload. Telemetry only: the session tally keeps sessionIDFromToolCall's bucket.
func telemetrySessionIDFromToolCall(ev *agenthooks.ToolCallEvent) string {
switch e := ev.Raw.(type) {
case *claude.PreToolUseEvent:
return e.SessionID
case *cursor.ShellPreEvent:
return e.ConversationID
case *copilotcli.PreToolUseEvent:
return e.SessionID
case *copilot.PreToolUseEvent:
return e.SessionID
case *codex.PreToolUseEvent:
return e.SessionID
case *gemini.BeforeToolEvent:
return e.SessionID
default:
return ""
}
}

// cxBeforeToolCall gates shell execution against the organization's blacklist,
// tool rules, and the SCA guardrail (malicious / vulnerable package installs).
func cxBeforeToolCall(ev agenthooks.ToolCallEvent) agenthooks.ToolVerdict {
Expand All @@ -87,9 +115,10 @@ func cxBeforeToolCall(ev agenthooks.ToolCallEvent) agenthooks.ToolVerdict {
if scaScanner != nil && scannerOn(ev.Agent, scannerconfig.SCA) {
agent := agentToString(ev.Agent)
sid := sessionIDFromToolCall(&ev)
if finding, remediation, severity := scaScanner.CheckBashInstall(ev.Command, ev.WorkDir, agent, sid); finding != "" && !belowThreshold(ev.Agent, severity) {
telemetrySID := telemetrySessionIDFromToolCall(&ev)
if finding, remediation, severity := scaScanner.CheckBashInstall(ev.Command, ev.WorkDir, agent, telemetrySID); finding != "" && !belowThreshold(ev.Agent, severity) {
sessiontally.Add(sid, engineSca, 1, 1)
logRemediationTelemetry(agent, "SCA", severity, sid)
logRemediationTelemetry(agent, hooktelemetry.EngineOSS, severity, telemetrySID, ev.WorkDir, remediation)
return agenthooks.DenyWithContext(finding, remediation)
}
}
Expand Down Expand Up @@ -145,7 +174,7 @@ func cxBeforeFileEdit(ev agenthooks.FileEditEvent) agenthooks.FileEditVerdict {
if scannerOn(ev.Agent, scannerconfig.ASCA) {
if blocked, reason, context, severity := asca.ScanFileEdit(&ev, telemetryWrapper, agent); blocked && !belowThreshold(ev.Agent, severity) {
sessiontally.Add(ev.SessionID, engineAsca, 1, 1)
logRemediationTelemetry(agent, "Asca", severity, ev.SessionID)
logRemediationTelemetry(agent, "Asca", severity, ev.SessionID, ev.WorkDir, context)
return agenthooks.RejectEditWithContext(reason, context)
}
}
Expand All @@ -154,7 +183,7 @@ func cxBeforeFileEdit(ev agenthooks.FileEditEvent) agenthooks.FileEditVerdict {
blocked, reason, context, note, severity := kics.ScanFileEdit(&ev, kicsScanner, telemetryWrapper, agent)
if blocked && !belowThreshold(ev.Agent, severity) {
sessiontally.Add(ev.SessionID, engineKics, 1, 1)
logRemediationTelemetry(agent, "IaC", severity, ev.SessionID)
logRemediationTelemetry(agent, "IaC", severity, ev.SessionID, ev.WorkDir, context)
return agenthooks.RejectEditWithContext(reason, context)
}
kicsNote = note
Expand All @@ -163,15 +192,15 @@ func cxBeforeFileEdit(ev agenthooks.FileEditEvent) agenthooks.FileEditVerdict {
for _, diff := range ev.Changes {
if finding, remediation, severity := scaScanner.CheckManifestEdit(ev.FilePath, fullAfterContent(ev.FilePath, diff), ev.WorkDir, agent, ev.SessionID); finding != "" && !belowThreshold(ev.Agent, severity) {
sessiontally.Add(ev.SessionID, engineSca, 1, 1)
logRemediationTelemetry(agent, "Oss", severity, ev.SessionID)
logRemediationTelemetry(agent, hooktelemetry.EngineOSS, severity, ev.SessionID, ev.WorkDir, remediation)
return agenthooks.RejectEditWithContext(finding, remediation)
}
}
}
if secretsScanner != nil && secretsApply(ev.Agent) && scannerOn(ev.Agent, scannerconfig.Secrets) {
if blocked, reason, context, severity := secrets.ScanFileEdit(&ev, secretsScanner, telemetryWrapper, agent); blocked && !belowThreshold(ev.Agent, severity) {
sessiontally.Add(ev.SessionID, engineSecrets, 1, 1)
logRemediationTelemetry(agent, "Secrets", severity, ev.SessionID)
logRemediationTelemetry(agent, "Secrets", severity, ev.SessionID, ev.WorkDir, context)
return agenthooks.RejectEditWithContext(reason, context)
}
}
Expand Down Expand Up @@ -265,7 +294,11 @@ func promptWorkspaceRoots(raw any) []string {
// RegisterGuardrails wires the four guardrail handlers and instantiates the
// SCA, KICS, and secret-detection scanners used by the Bash and FileEdit handlers.
func RegisterGuardrails(jwt wrappers.JWTWrapper, ff wrappers.FeatureFlagsWrapper, rt wrappers.RealtimeScannerWrapper, tel wrappers.TelemetryWrapper) {
if transport, ok := tel.(*wrappers.TelemetryHTTPWrapper); ok {
tel = transport.ForAgentHooks()
}
scaScanner = sca.NewScanner(jwt, ff, rt)
scaScanner.Telemetry = tel
kicsScanner = kics.NewScanner(jwt, ff)
secretsScanner = secrets.NewScanner(jwt, ff)
telemetryWrapper = tel
Expand All @@ -288,11 +321,19 @@ func RegisterPassThrough() {
}

// logRemediationTelemetry sends telemetry when remediation context is delivered to the agent.
func logRemediationTelemetry(agent, engine, severity, sessionID string) {
func logRemediationTelemetry(agent, engine, severity, sessionID, workDir, guidance string) {
ids, count := hooktelemetry.TakeRemediationFindings()
if telemetryWrapper == nil {
return
}

attrs := wrappers.EventAttributes{"remediationMode": hooktelemetry.RemediationModeSelfHealing}
if ids != "" {
attrs["vulnerabilityId"] = ids
}
if sessionID != "" {
attrs["aiAgentSessionId"] = sessionID
}
telemetryData := &wrappers.DataForAITelemetry{
AIProvider: agent,
Agent: hostenv.TelemetryAgent(agent),
Expand All @@ -301,9 +342,19 @@ func logRemediationTelemetry(agent, engine, severity, sessionID string) {
UniqueID: wrappers.GetUniqueID(),
Type: "hooks-remediate",
SubType: "fixWithAIAssist",
StatusResult: hooktelemetry.RemediationSucceeded,
TotalCount: count,
ProblemSeverity: severity,
AiAgentSessionId: sessionID,
EventAttributes: &attrs,
}
if strings.TrimSpace(guidance) == "" {
telemetryData.Status = hooktelemetry.StatusFailure
telemetryData.StatusResult = hooktelemetry.FailureResult("Remediation", remediationInstructionsMissing, hooktelemetry.ErrorCodeNone)
telemetryData.TotalCount = 0
attrs["reasonCode"] = remediationInstructionsMissing
}
hooktelemetry.Enrich(telemetryData, workDir)

if err := telemetryWrapper.SendAIDataToLog(telemetryData); err != nil {
// fail-open
Expand All @@ -319,8 +370,9 @@ func switchesApply(agent agenthooks.AgentID) bool {
switch agent {
case agenthooks.AgentClaude, agenthooks.AgentCursor, agenthooks.AgentGemini, agenthooks.AgentCodex:
return false
default:
return true
}
return true
}

// secretsApply reports whether the secret-detection guardrail runs for this agent. Claude, Cursor, Gemini
Expand Down
47 changes: 37 additions & 10 deletions internal/commands/agenthooks/cx/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/kics"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/guardrails/secrets"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/hooktelemetry"
"github.com/checkmarx/ast-cli/internal/commands/agenthooks/sca"
"github.com/checkmarx/ast-cli/internal/services/realtimeengine"
"github.com/checkmarx/ast-cli/internal/services/realtimeengine/iacrealtime"
Expand Down Expand Up @@ -194,6 +195,7 @@ func TestCxBeforeToolCall_ToolRule_AsksUser(t *testing.T) {
}
}

//nolint:goconst // test fixtures repeat values on purpose
func TestCxBeforeToolCall_SCAFinding_DeniesWithContext(t *testing.T) {
resetHookGlobals(t)
tel := &recordingTelemetry{}
Expand Down Expand Up @@ -222,8 +224,8 @@ func TestCxBeforeToolCall_SCAFinding_DeniesWithContext(t *testing.T) {
if len(tel.calls) != 1 {
t.Fatalf("expected 1 telemetry call, got %d", len(tel.calls))
}
if tel.calls[0].Engine != "SCA" {
t.Errorf("Engine = %q, want SCA", tel.calls[0].Engine)
if tel.calls[0].Engine != "oss" || tel.calls[0].ScanType != "oss" {
t.Errorf("Engine/ScanType = %q/%q, want oss/oss", tel.calls[0].Engine, tel.calls[0].ScanType)
}
}

Expand Down Expand Up @@ -414,8 +416,8 @@ func TestCxBeforeFileEdit_SCAManifest_RejectsWithContext(t *testing.T) {
if len(tel.calls) != 1 {
t.Fatalf("expected 1 telemetry call, got %d", len(tel.calls))
}
if tel.calls[0].Engine != "Oss" {
t.Errorf("Engine = %q, want Oss", tel.calls[0].Engine)
if tel.calls[0].Engine != "oss" || tel.calls[0].ScanType != "oss" {
t.Errorf("Engine/ScanType = %q/%q, want oss/oss", tel.calls[0].Engine, tel.calls[0].ScanType)
}
}

Expand Down Expand Up @@ -623,23 +625,26 @@ func TestRegisterGuardrails_AndPassThrough(t *testing.T) {
}
}

//nolint:goconst // test fixtures repeat values on purpose
func TestLogRemediationTelemetry(t *testing.T) {
resetHookGlobals(t)

t.Run("nil_wrapper_noop", func(t *testing.T) {
telemetryWrapper = nil
logRemediationTelemetry("Claude", "SCA", "High", "s1") // must not panic
logRemediationTelemetry("Claude", "SCA", "High", "s1", "", "guidance") // must not panic
})

t.Run("sends_payload", func(t *testing.T) {
tel := &recordingTelemetry{}
telemetryWrapper = tel
logRemediationTelemetry("Cursor", "Asca", "Critical", "sess-9")
const ruleID = "1027"
hooktelemetry.SetRemediationFindings([]string{ruleID, ruleID}, 2)
logRemediationTelemetry("Cursor", "Asca", "Critical", "sess-9", "", "guidance")
if len(tel.calls) != 1 {
t.Fatalf("got %d calls, want 1", len(tel.calls))
}
got := tel.calls[0]
if got.AIProvider != "Cursor" || got.Agent != "Cursor-cli" {
if got.AIProvider != "Cursor" || got.Agent != "Cursor-CLI" {
t.Errorf("AIProvider/Agent = %q/%q", got.AIProvider, got.Agent)
}
if got.Engine != "Asca" || got.ScanType != "asca" {
Expand All @@ -648,6 +653,11 @@ func TestLogRemediationTelemetry(t *testing.T) {
if got.Type != telemetryTypeHooksRemediate || got.SubType != "fixWithAIAssist" {
t.Errorf("Type/SubType = %q/%q", got.Type, got.SubType)
}
assert.Equal(t, 2, got.TotalCount)
assert.Equal(t, ruleID, (*got.EventAttributes)["vulnerabilityId"])
if got.StatusResult != "Remediation completed successfully." {
t.Errorf("StatusResult = %q", got.StatusResult)
}
if got.ProblemSeverity != "Critical" || got.AiAgentSessionId != "sess-9" {
t.Errorf("severity/session = %q/%q", got.ProblemSeverity, got.AiAgentSessionId)
}
Expand All @@ -656,7 +666,7 @@ func TestLogRemediationTelemetry(t *testing.T) {
t.Run("send_error_fail_open", func(t *testing.T) {
tel := &recordingTelemetry{err: os.ErrPermission}
telemetryWrapper = tel
logRemediationTelemetry("Claude", "SCA", "High", "s2") // must not panic
logRemediationTelemetry("Claude", "SCA", "High", "s2", "", "guidance") // must not panic
if len(tel.calls) != 1 {
t.Fatalf("got %d calls, want 1", len(tel.calls))
}
Expand Down Expand Up @@ -858,7 +868,7 @@ func TestLogRemediationTelemetry_NilWrapper_NoOp(t *testing.T) {

telemetryWrapper = nil
assert.NotPanics(t, func() {
logRemediationTelemetry("Claude", "SCA", "finding", "remediation")
logRemediationTelemetry("Claude", "SCA", "finding", "remediation", "", "guidance")
})
}

Expand All @@ -868,7 +878,7 @@ func TestLogRemediationTelemetry_WithWrapper_Sends(t *testing.T) {

telemetryWrapper = mock.TelemetryMockWrapper{}
assert.NotPanics(t, func() {
logRemediationTelemetry("Claude", "SCA", "finding", "remediation")
logRemediationTelemetry("Claude", "SCA", "finding", "remediation", "", "guidance")
})
}

Expand Down Expand Up @@ -964,3 +974,20 @@ func TestCodexApplyPatch_KICSFinding_DeniesEndToEnd(t *testing.T) {
assert.Contains(t, out, `"permissionDecision":"deny"`)
assert.Contains(t, out, "KICS")
}

func TestLogRemediationTelemetry_MissingGuidance(t *testing.T) {
resetHookGlobals(t)
tel := &recordingTelemetry{}
telemetryWrapper = tel
hooktelemetry.SetRemediationFindings([]string{"rule-id"}, 1)
logRemediationTelemetry("Copilot", "Asca", "High", "session", "", "")
if len(tel.calls) != 1 {
t.Fatalf("got %d events, want 1", len(tel.calls))
}
event := tel.calls[0]
if event.Status != "Failure" || event.TotalCount != 0 ||
event.StatusResult != "Remediation failed. Reason: remediation_instructions_missing, Error Code: None" ||
(*event.EventAttributes)["reasonCode"] != "remediation_instructions_missing" {
t.Fatalf("unexpected remediation failure: %+v", event)
}
}
Loading
Loading