9e43d07428
- store.GuardrailEvent 表(sundynix_guardrail_event) + AppendGuardrailEvent/ListGuardrailEvents - middleware.Guardrail(db):命中 blocked/suspect 时 best-effort 落库 (actor/kind/reason/signals/method/path/ip,独立超时 ctx) - GET /api/v1/admin/guardrail-events:安全事件流(倒序,翻页) - store.clampPage 抽出分页归一(audit/guardrail 共用) - live:注入 "ignore all previous instructions" → 422 硬拦 + 事件留痕(kind=blocked) - DEPTH_ROADMAP T4.B 护栏事件打勾 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package store
|
|
|
|
import "context"
|
|
|
|
// AppendAudit 追加一条审计留痕(best-effort:审计失败不应影响主流程,调用方忽略返回)。
|
|
func (p *Postgres) AppendAudit(ctx context.Context, a *AuditLog) error {
|
|
if p.db == nil || a == nil {
|
|
return errStoreDisabled
|
|
}
|
|
return p.db.WithContext(ctx).Create(a).Error
|
|
}
|
|
|
|
// ListAudit 倒序列出审计留痕(管理端审计流;limit 限流、offset 翻页)。
|
|
func (p *Postgres) ListAudit(ctx context.Context, limit, offset int) ([]AuditLog, error) {
|
|
if p.db == nil {
|
|
return nil, errStoreDisabled
|
|
}
|
|
limit, offset = clampPage(limit, offset)
|
|
var out []AuditLog
|
|
err := p.db.WithContext(ctx).Order("created_at desc").Limit(limit).Offset(offset).Find(&out).Error
|
|
return out, err
|
|
}
|
|
|
|
// AppendGuardrailEvent 追加一条护栏命中(best-effort)。
|
|
func (p *Postgres) AppendGuardrailEvent(ctx context.Context, e *GuardrailEvent) error {
|
|
if p.db == nil || e == nil {
|
|
return errStoreDisabled
|
|
}
|
|
return p.db.WithContext(ctx).Create(e).Error
|
|
}
|
|
|
|
// ListGuardrailEvents 倒序列出护栏事件(管理端安全事件流)。
|
|
func (p *Postgres) ListGuardrailEvents(ctx context.Context, limit, offset int) ([]GuardrailEvent, error) {
|
|
if p.db == nil {
|
|
return nil, errStoreDisabled
|
|
}
|
|
limit, offset = clampPage(limit, offset)
|
|
var out []GuardrailEvent
|
|
err := p.db.WithContext(ctx).Order("created_at desc").Limit(limit).Offset(offset).Find(&out).Error
|
|
return out, err
|
|
}
|
|
|
|
// clampPage 归一分页参数:limit ∈ [1,200](默认 50),offset ≥ 0。
|
|
func clampPage(limit, offset int) (int, int) {
|
|
if limit <= 0 || limit > 200 {
|
|
limit = 50
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
return limit, offset
|
|
}
|