The /admin/guard page, a Guardrails link, and the policies in force on the profile page
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7e18b8ab53
commit
02aac84ca5
11 files changed
+305
-3
No files matched your search
@@ -37,3 +37,55 @@ func (s *DB) DetachGuard(ctx context.Context, a guard.Attachment) (bool, error)
|
||||
n, _ := res.RowsAffected()
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// GuardEvent is one request the guardrails acted on, by name only.
|
||||
type GuardEvent struct {
|
||||
At time.Time
|
||||
RequestID string
|
||||
KeyID string
|
||||
UserID string
|
||||
ProjectID string
|
||||
Model string
|
||||
Status int
|
||||
Action string
|
||||
Policy string
|
||||
Category string
|
||||
GuardMs int64
|
||||
}
|
||||
|
||||
// GuardEvents are the newest requests the guardrails did something to —
|
||||
// flagged, redacted, blocked, or would have in shadow — since a moment.
|
||||
func (s *DB) GuardEvents(ctx context.Context, since time.Time, limit int) ([]GuardEvent, error) {
|
||||
return queryAll(ctx, s.db, `SELECT ts, request_id, key_id, COALESCE(user_id, ''), project_id, model, status,
|
||||
guard_action, guard_policy, guard_category, guard_ms
|
||||
FROM usage WHERE ts >= ? AND guard_action <> '' ORDER BY ts DESC LIMIT ?`,
|
||||
[]any{millis(since), limit},
|
||||
func(rows *sql.Rows) (GuardEvent, error) {
|
||||
var e GuardEvent
|
||||
var ts int64
|
||||
err := rows.Scan(&ts, &e.RequestID, &e.KeyID, &e.UserID, &e.ProjectID, &e.Model, &e.Status,
|
||||
&e.Action, &e.Policy, &e.Category, &e.GuardMs)
|
||||
e.At = fromMillis(ts)
|
||||
return e, err
|
||||
})
|
||||
}
|
||||
|
||||
// GuardCount is how many requests one combination covers.
|
||||
type GuardCount struct {
|
||||
ProjectID, Action, Policy, Category string
|
||||
Count int64
|
||||
}
|
||||
|
||||
// GuardCounts totals what the guardrails did since a moment, by project,
|
||||
// action, policy and category.
|
||||
func (s *DB) GuardCounts(ctx context.Context, since time.Time) ([]GuardCount, error) {
|
||||
return queryAll(ctx, s.db, `SELECT project_id, guard_action, guard_policy, guard_category, COUNT(*)
|
||||
FROM usage WHERE ts >= ? AND guard_action <> ''
|
||||
GROUP BY project_id, guard_action, guard_policy, guard_category
|
||||
ORDER BY COUNT(*) DESC`, []any{millis(since)},
|
||||
func(rows *sql.Rows) (GuardCount, error) {
|
||||
var c GuardCount
|
||||
err := rows.Scan(&c.ProjectID, &c.Action, &c.Policy, &c.Category, &c.Count)
|
||||
return c, err
|
||||
})
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
|
||||
)
|
||||
@@ -25,3 +26,27 @@ func TestAGuardPolicyIsAttachedOnceAndDetached(t *testing.T) {
|
||||
t.Error("detached twice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhatTheGuardrailsDidCanBeReadBack(t *testing.T) {
|
||||
db := capacityStore(t)
|
||||
ctx := t.Context()
|
||||
now := time.Now()
|
||||
rows := []Usage{
|
||||
{At: now, KeyID: "k", ProjectID: "p1", Model: "m", ServedModel: "m", Status: 400, RequestID: "r1",
|
||||
GuardAction: "blocked", GuardPolicy: "base", GuardCategory: "secret", GuardMs: 3},
|
||||
{At: now, KeyID: "k", ProjectID: "p1", Model: "m", ServedModel: "m", Status: 200, RequestID: "r2",
|
||||
GuardAction: "blocked", GuardPolicy: "base", GuardCategory: "secret"},
|
||||
{At: now, KeyID: "k", ProjectID: "p2", Model: "m", ServedModel: "m", Status: 200, RequestID: "r3"},
|
||||
}
|
||||
if err := db.WriteUsage(ctx, rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ev, err := db.GuardEvents(ctx, now.Add(-time.Hour), 10)
|
||||
if err != nil || len(ev) != 2 || ev[0].Category != "secret" {
|
||||
t.Fatalf("%+v %v", ev, err)
|
||||
}
|
||||
c, err := db.GuardCounts(ctx, now.Add(-time.Hour))
|
||||
if err != nil || len(c) != 1 || c[0].Count != 2 || c[0].ProjectID != "p1" {
|
||||
t.Errorf("%+v %v", c, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user