-
Notifications
You must be signed in to change notification settings - Fork 42
feat(ui): show DefenseClaw enforcement spans in Monitor #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vineethsai7
wants to merge
3
commits into
agentcontrol:feature/68338-add-defense-claw-evaluator
Choose a base branch
from
vineethsai7:codex/defenseclaw-monitor-spans
base: feature/68338-add-defense-claw-evaluator
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| import { api } from '@/core/api/client'; | ||
| import type { components } from '@/core/api/generated/api-types'; | ||
|
|
||
| export type ControlExecutionEvent = | ||
| components['schemas']['ControlExecutionEvent']; | ||
| export type EventQueryResponse = components['schemas']['EventQueryResponse']; | ||
|
|
||
| export function useAgentEvents( | ||
| agentName: string, | ||
| eventWindowMs: number, | ||
| options?: { enabled?: boolean; refetchInterval?: number } | ||
| ) { | ||
| return useQuery({ | ||
| queryKey: ['agent-monitor-events', agentName, eventWindowMs], | ||
| queryFn: async (): Promise<EventQueryResponse> => { | ||
| const { data, error } = await api.observability.queryEvents({ | ||
| agent_name: agentName, | ||
| start_time: new Date(Date.now() - eventWindowMs).toISOString(), | ||
| limit: 20, | ||
| offset: 0, | ||
| }); | ||
|
|
||
| if (error) { | ||
| throw new Error('Failed to fetch recent executions'); | ||
| } | ||
|
|
||
| return data; | ||
| }, | ||
| enabled: | ||
| options?.enabled !== false && | ||
| !!agentName && | ||
| Number.isFinite(eventWindowMs) && | ||
| eventWindowMs > 0, | ||
| refetchInterval: options?.refetchInterval ?? 5000, | ||
| refetchIntervalInBackground: false, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 249 additions & 0 deletions
249
ui/src/core/page-components/agent-detail/monitor/recent-executions.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| Accordion, | ||
| Badge, | ||
| Card, | ||
| Code, | ||
| Group, | ||
| SimpleGrid, | ||
| Stack, | ||
| Text, | ||
| Title, | ||
| } from '@mantine/core'; | ||
| import { useState } from 'react'; | ||
|
|
||
| import type { ControlExecutionEvent } from '@/core/hooks/query-hooks/use-agent-events'; | ||
|
|
||
| type RecentExecutionsProps = { | ||
| events: ControlExecutionEvent[]; | ||
| hasError?: boolean; | ||
| }; | ||
|
|
||
| type EventMetadata = Record<string, unknown>; | ||
|
|
||
| function asRecord(value: unknown): EventMetadata | null { | ||
| return value !== null && typeof value === 'object' && !Array.isArray(value) | ||
| ? (value as EventMetadata) | ||
| : null; | ||
| } | ||
|
|
||
| function asString(value: unknown): string | null { | ||
| return typeof value === 'string' && value.length > 0 ? value : null; | ||
| } | ||
|
|
||
| function executionKey(event: ControlExecutionEvent): string { | ||
| return ( | ||
| event.control_execution_id || | ||
| [ | ||
| event.trace_id || 'no-trace', | ||
| event.span_id || 'no-span', | ||
| event.control_id, | ||
| event.timestamp || 'no-time', | ||
| ].join(':') | ||
| ); | ||
| } | ||
|
|
||
| function Detail({ label, value }: { label: string; value: string }) { | ||
| return ( | ||
| <Stack gap={2}> | ||
| <Text size="xs" c="dimmed" tt="uppercase" fw={600}> | ||
| {label} | ||
| </Text> | ||
| <Code style={{ overflowWrap: 'anywhere' }}>{value}</Code> | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
| export function RecentExecutions({ | ||
| events, | ||
| hasError = false, | ||
| }: RecentExecutionsProps) { | ||
| const latestKey = events.length > 0 ? executionKey(events[0]) : null; | ||
| const [selectedValue, setSelectedValue] = useState<string | null>(null); | ||
| const openValue = selectedValue ?? latestKey; | ||
|
|
||
| if (hasError) { | ||
| return ( | ||
| <Card withBorder p="md"> | ||
| <Title order={3} size="h5"> | ||
| Recent executions | ||
| </Title> | ||
| <Text size="sm" c="red" mt="xs"> | ||
| Failed to load recent executions. | ||
| </Text> | ||
| </Card> | ||
| ); | ||
| } | ||
|
|
||
| if (events.length === 0) { | ||
| return ( | ||
| <Card withBorder p="md"> | ||
| <Title order={3} size="h5"> | ||
| Recent executions | ||
| </Title> | ||
| <Text size="sm" c="dimmed" mt="xs"> | ||
| Exact control spans will appear here as they are received. | ||
| </Text> | ||
| </Card> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <Card withBorder p="md"> | ||
| <Group justify="space-between" mb="sm"> | ||
| <Title order={3} size="h5"> | ||
| Recent executions | ||
| </Title> | ||
| <Text size="xs" c="dimmed"> | ||
| Latest span opens automatically | ||
| </Text> | ||
| </Group> | ||
| <Accordion | ||
| value={openValue} | ||
| onChange={setSelectedValue} | ||
| variant="separated" | ||
| > | ||
| {events.map((event) => { | ||
| const metadata = (event.metadata ?? {}) as EventMetadata; | ||
| const blockedInput = asRecord(metadata.blocked_input); | ||
| const prompt = asString(blockedInput?.prompt); | ||
| const rawRequestBody = asString(blockedInput?.raw_request_body); | ||
| const verdictReason = asString(metadata.verdict_reason); | ||
| const ruleIds = Array.isArray(metadata.rule_ids) | ||
| ? metadata.rule_ids.filter( | ||
| (value): value is string => typeof value === 'string' | ||
| ) | ||
| : []; | ||
| const requestId = asString(metadata.request_id); | ||
| const key = executionKey(event); | ||
| const contentUnredacted = metadata.content_unredacted === true; | ||
|
|
||
| return ( | ||
| <Accordion.Item key={key} value={key}> | ||
| <Accordion.Control> | ||
| <Group justify="space-between" wrap="nowrap" pr="md"> | ||
| <Stack gap={1}> | ||
| <Text size="sm" fw={600}> | ||
| {event.control_name} | ||
| </Text> | ||
| <Text size="xs" c="dimmed"> | ||
| {event.timestamp | ||
| ? new Date(event.timestamp).toLocaleString() | ||
| : 'Timestamp unavailable'} | ||
| </Text> | ||
| </Stack> | ||
| <Group gap="xs" wrap="nowrap"> | ||
| {contentUnredacted ? ( | ||
| <Badge color="orange" variant="light"> | ||
| Unredacted | ||
| </Badge> | ||
| ) : null} | ||
| {!contentUnredacted ? ( | ||
| <Badge color="gray" variant="light"> | ||
| Metadata only | ||
| </Badge> | ||
| ) : null} | ||
| <Badge color={event.action === 'deny' ? 'red' : 'blue'}> | ||
| {event.action} | ||
| </Badge> | ||
| </Group> | ||
| </Group> | ||
| </Accordion.Control> | ||
| <Accordion.Panel> | ||
| <Stack gap="md"> | ||
| <SimpleGrid cols={{ base: 1, sm: 2, lg: 3 }} spacing="md"> | ||
| <Detail label="Trace ID" value={event.trace_id} /> | ||
| <Detail label="Span ID" value={event.span_id} /> | ||
| <Detail | ||
| label="Request ID" | ||
| value={requestId ?? 'Unavailable'} | ||
| /> | ||
| <Detail | ||
| label="Rule IDs" | ||
| value={ | ||
| ruleIds.length > 0 ? ruleIds.join(', ') : 'Unavailable' | ||
| } | ||
| /> | ||
| <Detail label="Stage" value={event.check_stage} /> | ||
| <Detail | ||
| label="Duration" | ||
| value={ | ||
| event.execution_duration_ms == null | ||
| ? 'Unavailable' | ||
| : `${event.execution_duration_ms} ms` | ||
| } | ||
| /> | ||
| </SimpleGrid> | ||
|
|
||
| {prompt !== null ? ( | ||
| <Stack gap="xs"> | ||
| <Group gap="xs"> | ||
| <Text size="sm" fw={600}> | ||
| Blocked input | ||
| </Text> | ||
| <Badge | ||
| color={contentUnredacted ? 'orange' : 'gray'} | ||
| variant="light" | ||
| > | ||
| {contentUnredacted | ||
| ? 'Unredacted content' | ||
| : 'Redacted content'} | ||
| </Badge> | ||
| </Group> | ||
| <Code | ||
| block | ||
| style={{ | ||
| whiteSpace: 'pre-wrap', | ||
| overflowWrap: 'anywhere', | ||
| }} | ||
| > | ||
| {prompt} | ||
| </Code> | ||
| </Stack> | ||
| ) : null} | ||
|
|
||
| {verdictReason !== null ? ( | ||
| <Stack gap="xs"> | ||
| <Text size="sm" fw={600}> | ||
| Enforcement reason | ||
| </Text> | ||
| <Code | ||
| block | ||
| style={{ | ||
| whiteSpace: 'pre-wrap', | ||
| overflowWrap: 'anywhere', | ||
| }} | ||
| > | ||
| {verdictReason} | ||
| </Code> | ||
| </Stack> | ||
| ) : null} | ||
|
|
||
| {rawRequestBody !== null ? ( | ||
| <Accordion variant="contained"> | ||
| <Accordion.Item value="raw-request"> | ||
| <Accordion.Control>Raw request body</Accordion.Control> | ||
| <Accordion.Panel> | ||
| <Code | ||
| block | ||
| style={{ | ||
| whiteSpace: 'pre-wrap', | ||
| overflowWrap: 'anywhere', | ||
| }} | ||
| > | ||
| {rawRequestBody} | ||
| </Code> | ||
| </Accordion.Panel> | ||
| </Accordion.Item> | ||
| </Accordion> | ||
| ) : null} | ||
| </Stack> | ||
| </Accordion.Panel> | ||
| </Accordion.Item> | ||
| ); | ||
| })} | ||
| </Accordion> | ||
| </Card> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.