Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion agent/auto_scope_hygiene_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestTaskScopeLabelIsHonest(t *testing.T) {
// denominator and list_untested_assets.
func TestAutoScopeOutsideDeclaredRootExcluded(t *testing.T) {
d := testDB(t)
defer d.Close()
t.Cleanup(func() { _ = d.Close() })

stamp := time.Now().UnixNano()
declared := fmt.Sprintf("app.f8-%d.invalid", stamp)
Expand Down
4 changes: 2 additions & 2 deletions agent/blackboard_inheritance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func companiesName(t *testing.T, companies *db.CompanyStore, companyID int64) st

func TestBlackboardToolsReadDirectSources(t *testing.T) {
d := testDB(t)
defer d.Close()
t.Cleanup(func() { _ = d.Close() })

grand, err := d.CreateTask("grand", "grand goal", nil, 0, 0)
if err != nil {
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestBlackboardToolsReadDirectSources(t *testing.T) {
// model which ids it deferred, after de-duplicating and dropping invalid ids.
func TestGetWorkerTraceStepIDsDegradeGracefully(t *testing.T) {
d := testDB(t)
defer d.Close()
t.Cleanup(func() { _ = d.Close() })

task, err := d.CreateTask("trace-cap", "goal", nil, 0, 0)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion agent/coverage_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// write happens even when the stored flag is off, which is the outage behaviour.
func TestCoverageReadFailureKeepsToolsAndWritesAutoScope(t *testing.T) {
d := testDB(t)
defer d.Close()
t.Cleanup(func() { _ = d.Close() })
off := false
task, err := d.CreateTaskWithOptions("coverage outage", "goal", db.TaskCreateOptions{CoverageEnabled: &off})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion agent/finding_reasoning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func TestReportFindingStoresReasoningFields(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteTask(task.ID)
defer func() {
_, _ = d.DeleteFindingsByTask(task.ID)
_ = d.DeleteTask(task.ID)
}()
ts := NewToolSet(nil, "")
ts.ts, ts.taskID = d.Exploration(task.ExplorationID), task.ID
for _, tc := range []struct{ extra, counter, rationale string }{
Expand Down
10 changes: 8 additions & 2 deletions agent/finding_recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func TestReportFindingOptionalTrafficWithoutCapture(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteTask(task.ID)
defer func() {
_, _ = d.DeleteFindingsByTask(task.ID)
_ = d.DeleteTask(task.ID)
}()
ts := NewToolSet(nil, "")
ts.ts, ts.taskID = d.Exploration(task.ExplorationID), task.ID
ts.SetFindingRecorder(evidence.New(d, nil, t.TempDir()))
Expand Down Expand Up @@ -60,7 +63,10 @@ func TestReportFindingAtomicContract(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteTask(task.ID)
defer func() {
_, _ = d.DeleteFindingsByTask(task.ID)
_ = d.DeleteTask(task.ID)
}()
ts := NewToolSet(nil, "")
ts.ts = d.Exploration(task.ExplorationID)
ts.taskID = task.ID
Expand Down
6 changes: 3 additions & 3 deletions agent/scan_ingest_tool_pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestIngestScanOutputPG(t *testing.T) {
if err != nil {
t.Fatal(err)
}
const assetsWhere = `ip IN ('198.51.100.77','2001:db8::77') OR domain LIKE '%scan-ingest.example.com' OR url LIKE '%198.51.100.77%' OR url LIKE '%2001:db8::77%'`
const assetsWhere = `ip IN ('198.51.100.77','2001:db8::77') OR domain IN ('198.51.100.77','2001:db8::77','example.com') OR domain LIKE '%scan-ingest.example.com' OR url LIKE '%198.51.100.77%' OR url LIKE '%2001:db8::77%'`
t.Cleanup(func() {
_ = d.DeleteTask(task.ID)
_, _ = d.Exec(`DELETE FROM assets WHERE ` + assetsWhere)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestIngestScanOutputJSONPG(t *testing.T) {
if err != nil {
t.Fatal(err)
}
const assetsWhere = `domain LIKE '%scan-ingest.example.com' OR url LIKE '%scan-ingest.example.com%'`
const assetsWhere = `ip IN ('198.51.100.77','2001:db8::77') OR domain IN ('198.51.100.77','2001:db8::77','example.com') OR domain LIKE '%scan-ingest.example.com' OR url LIKE '%scan-ingest.example.com%'`
t.Cleanup(func() {
_ = d.DeleteTask(task.ID)
_, _ = d.Exec(`DELETE FROM assets WHERE ` + assetsWhere)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestIngestScanOutputUserinfoPG(t *testing.T) {
if err != nil {
t.Fatal(err)
}
const assetsWhere = `domain LIKE '%scan-ingest.example.com' OR url LIKE '%scan-ingest.example.com%'`
const assetsWhere = `ip IN ('198.51.100.77','2001:db8::77') OR domain IN ('198.51.100.77','2001:db8::77','example.com') OR domain LIKE '%scan-ingest.example.com' OR url LIKE '%scan-ingest.example.com%'`
t.Cleanup(func() {
_ = d.DeleteTask(task.ID)
_, _ = d.Exec(`DELETE FROM assets WHERE ` + assetsWhere)
Expand Down
4 changes: 2 additions & 2 deletions db/activity_outbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFlushActivityOutboxReplaysOnce(t *testing.T) {
if err != nil {
SkipPGUnavailable(t, err)
}
defer d.Close()
t.Cleanup(func() { _ = d.Close() })
expID, err := d.CreateExploration("outbox", "replay")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestFlushActivityOutboxDropsStuckRow(t *testing.T) {
if err != nil {
SkipPGUnavailable(t, err)
}
defer d.Close()
t.Cleanup(func() { _ = d.Close() })
const missing = int64(9_000_000_000_001)
t.Cleanup(func() { _, _ = d.Exec(`DELETE FROM activity_outbox WHERE exploration_id=$1`, missing) })
es := d.Exploration(missing)
Expand Down
1 change: 1 addition & 0 deletions db/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ func TestAssetIPRejectsHostname(t *testing.T) {
t.Fatalf("empty ip rejected: %v", err)
}
deleteAsset(d, id)
_, _ = d.Exec(`DELETE FROM assets WHERE domain IN ('ip-guard.example.com','example.com')`)
id, err = av2.UpsertIP(UpsertIPReq{IP: "203.0.113.7"})
if err != nil {
t.Fatalf("valid ip rejected: %v", err)
Expand Down
96 changes: 96 additions & 0 deletions db/dashboard_usage_indexes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package db

import (
"fmt"
"strings"
"testing"
"time"
)

func TestDashboardUsageIndexesMigration0010(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
SkipPGUnavailable(t, err)
}
t.Cleanup(func() { _ = d.Close() })

wantIndexes := []struct {
name string
table string
pred string
}{
{"idx_act_result_created", "activity", "kind = 'result'"},
{"idx_conv_act_result", "conversation_activities", "kind = 'result'"},
{"idx_llm_usage_judge_ts", "llm_usage", "worker = 'judge'"},
}

for _, want := range wantIndexes {
var valid, ready bool
var def string
err := d.QueryRow(`
SELECT i.indisvalid, i.indisready, pg_get_indexdef(i.indexrelid)
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relname = $1`, want.name).Scan(&valid, &ready, &def)
if err != nil {
t.Fatalf("index public.%s missing after Open: %v", want.name, err)
}
if !valid || !ready {
t.Fatalf("index %s indisvalid=%v indisready=%v, want both true (%s)", want.name, valid, ready, def)
}
if !strings.Contains(def, want.table) || !strings.Contains(def, want.pred) {
t.Fatalf("index %s definition %q missing table %q or predicate %q", want.name, def, want.table, want.pred)
}
}

// Verify sargable TokenByModel matches both specific task_id and empty task_id.
taskID := fmt.Sprintf("idx-test-%d", time.Now().UnixNano())
t.Cleanup(func() {
_, _ = d.Exec(`DELETE FROM llm_usage WHERE task_id = $1 OR profile_name = $2`, taskID, taskID)
})
if err := d.InsertLLMUsage(&LLMUsage{
TaskID: taskID,
Worker: "worker",
Model: "claude-sonnet-4",
ProfileName: taskID,
InputTokens: 1000,
OutputTokens: 200,
Status: "ok",
}); err != nil {
t.Fatal(err)
}
if err := d.InsertLLMUsage(&LLMUsage{
TaskID: "",
Worker: "chat",
Model: "gpt-5",
ProfileName: taskID,
InputTokens: 500,
OutputTokens: 100,
Status: "ok",
}); err != nil {
t.Fatal(err)
}

got, err := d.TokenByModel(taskID)
if err != nil {
t.Fatalf("TokenByModel(%q): %v", taskID, err)
}
if len(got) != 1 || got[0].Model != "claude-sonnet-4" || got[0].InputTokens != 1000 || got[0].OutputTokens != 200 {
t.Fatalf("TokenByModel(%q) = %+v, want 1 claude-sonnet-4 row", taskID, got)
}

emptyGot, err := d.TokenByModel("")
if err != nil {
t.Fatalf("TokenByModel(\"\"): %v", err)
}
foundChat := false
for _, m := range emptyGot {
if m.Model == "gpt-5" && m.InputTokens >= 500 {
foundChat = true
}
}
if !foundChat {
t.Fatalf("TokenByModel(\"\") did not include NULL/empty task_id row: %+v", emptyGot)
}
}
1 change: 1 addition & 0 deletions db/finding_quality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestFindingQualityByNodeID(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(rec.FindingID)
if err := d.SetFindingVerification(rec.FindingID, VerificationVerified, 3, "reproduced twice on staging"); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions db/finding_reasoning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func TestFindingReasoningFieldsRoundTrip(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(with.FindingID)
without, err := es.RecordFinding(t.Context(), RecordFindingInput{TaskID: tk.ID, VulnClass: "IDOR", Severity: "low", Summary: "without reasoning", Worker: "test"})
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(without.FindingID)
want := map[int64][2]string{
with.FindingID: {"UUID guessing returned 404", "two accounts, same response body"},
without.FindingID: {"", ""},
Expand Down
6 changes: 5 additions & 1 deletion db/finding_traffic_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func TestFindingTrafficLegacyArchiveDefaults(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer func() { d.Exec(`DELETE FROM task_archives WHERE task_id=$1`, task.ID); d.DeleteTask(task.ID) }()
defer func() {
_, _ = d.DeleteFindingsByTask(task.ID)
_, _ = d.Exec(`DELETE FROM task_archives WHERE task_id=$1`, task.ID)
_ = d.DeleteTask(task.ID)
}()
f, err := d.Exploration(task.ExplorationID).RecordFinding(t.Context(), RecordFindingInput{TaskID: task.ID, ExplorationID: task.ExplorationID, Summary: "legacy", Severity: "low"})
if err != nil {
t.Fatal(err)
Expand Down
12 changes: 9 additions & 3 deletions db/findings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestSetFindingStatusMirrorsNodePayload(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(fid)

n, err := d.SetFindingStatus(fid, FindingFalsePositive)
if err != nil || n != 1 {
Expand Down Expand Up @@ -146,13 +147,13 @@ func TestFindingReportState(t *testing.T) {
if err != nil {
SkipPGUnavailable(t, err)
}
defer d.Close()
t.Cleanup(func() { d.Close() })

tk, err := d.CreateTask("finding report state", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer d.DeleteTask(tk.ID)
t.Cleanup(func() { _ = d.DeleteTask(tk.ID) })

es := d.Exploration(tk.ExplorationID)
nodeID, err := es.AddNode(KindFinding, map[string]any{"summary": "x", "severity": "high"}, 5, "confirmed", "worker", nil)
Expand All @@ -167,7 +168,11 @@ func TestFindingReportState(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { d.Exec(`DELETE FROM notification_events WHERE finding_id IN ($1,$2)`, fid, second) })
t.Cleanup(func() {
_, _ = d.Exec(`DELETE FROM notification_events WHERE finding_id IN ($1,$2)`, fid, second)
_, _ = d.DeleteFinding(fid)
_, _ = d.DeleteFinding(second)
})

one, err := d.GetFinding(fid)
if err != nil || one == nil || one.ReportState != FindingReportNone {
Expand Down Expand Up @@ -633,6 +638,7 @@ func TestAddFindingFollowUpIntent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(findingID)

auditInput := Activity{Worker: "system", Kind: "text", Summary: "Manually submitted vulnerability deep-exploitation intent", Detail: "Verify exploitability and build an evidence chain"}
intentID, audit, err := store.AddFindingFollowUpIntent(findingID, findingNodeID, "Verify exploitability and build an evidence chain", auditInput)
Expand Down
14 changes: 12 additions & 2 deletions db/lessons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,14 @@ func TestExtractLessonsScopesSingleCompanyFromTaskScope(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { cleanupErr(t, "extract task", d.DeleteTask(task.ID)) })
if _, err := d.Exec(`
var extractedFID int64
if err := d.QueryRow(`
INSERT INTO findings(task_id,vulnclass,name,summary,severity,status)
VALUES ($1,'xss','Reflected XSS','reflected in search','high','confirmed')`, task.ID); err != nil {
VALUES ($1,'xss','Reflected XSS','reflected in search','high','confirmed')
RETURNING id`, task.ID).Scan(&extractedFID); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, extractedFID) })
n, err := d.ExtractLessonsForTask(t.Context(), task.ID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -477,6 +480,7 @@ func TestExtractLessonsRetractsReversedDisposition(t *testing.T) {
RETURNING id`, task.ID, cue, label).Scan(&fid); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })

n, err := d.ExtractLessonsForTask(t.Context(), task.ID)
if err != nil {
Expand Down Expand Up @@ -589,12 +593,14 @@ func TestExtractLessonsRetireOnlyWhenLastEvidence(t *testing.T) {
RETURNING id`, task.ID, cue, label).Scan(&aID); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, aID) })
if err := d.QueryRow(`
INSERT INTO findings(task_id,vulnclass,name,summary,severity,status)
VALUES ($1,$2,'Reflected XSS',$3,'high','confirmed')
RETURNING id`, task.ID, cue, label).Scan(&bID); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, bID) })

if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -681,6 +687,7 @@ func TestExtractLessonsRetractsAllRetests(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })

r1 := insertLessonRetest(t, d, fid, "reproduced")
if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
Expand Down Expand Up @@ -752,6 +759,7 @@ func TestExtractLessonsDoesNotUnretireWithEvidence(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })

if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -798,6 +806,7 @@ func TestExtractLessonsLeavesAdopted(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })

if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -841,6 +850,7 @@ func TestExtractLessonsReopenRetractsNothing(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })

if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
Expand Down
Loading
Loading