ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
test: clean up residual findings, retest conversations, parent assets, and test rows
BaaRai79119ae9bde5
diff --git a/agent/auto_scope_hygiene_test.go b/agent/auto_scope_hygiene_test.goindex b4e9943..fccbd1e 100644--- a/agent/auto_scope_hygiene_test.go+++ b/agent/auto_scope_hygiene_test.go@@ -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)diff --git a/agent/blackboard_inheritance_test.go b/agent/blackboard_inheritance_test.goindex def03eb..cc8275b 100644--- a/agent/blackboard_inheritance_test.go+++ b/agent/blackboard_inheritance_test.go@@ -194,7 +194,7 @@ func companiesName(t *testing.T, companies *db.CompanyStore, companyID int64) stfunc 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 {@@ -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 {diff --git a/agent/coverage_flag_test.go b/agent/coverage_flag_test.goindex 7bcbf4b..17f2d04 100644--- a/agent/coverage_flag_test.go+++ b/agent/coverage_flag_test.go@@ -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 := falsetask, err := d.CreateTaskWithOptions("coverage outage", "goal", db.TaskCreateOptions{CoverageEnabled: &off})if err != nil {diff --git a/agent/finding_reasoning_test.go b/agent/finding_reasoning_test.goindex 88ce3a1..9876eec 100644--- a/agent/finding_reasoning_test.go+++ b/agent/finding_reasoning_test.go@@ -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.IDfor _, tc := range []struct{ extra, counter, rationale string }{diff --git a/agent/finding_recorder_test.go b/agent/finding_recorder_test.goindex 13d274c..0dbf147 100644--- a/agent/finding_recorder_test.go+++ b/agent/finding_recorder_test.go@@ -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.IDts.SetFindingRecorder(evidence.New(d, nil, t.TempDir()))@@ -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.IDdiff --git a/agent/scan_ingest_tool_pg_test.go b/agent/scan_ingest_tool_pg_test.goindex d952914..10c4a50 100644--- a/agent/scan_ingest_tool_pg_test.go+++ b/agent/scan_ingest_tool_pg_test.go@@ -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)diff --git a/db/assets_test.go b/db/assets_test.goindex 762a08e..ff1d7ef 100644--- a/db/assets_test.go+++ b/db/assets_test.go@@ -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)diff --git a/db/finding_quality_test.go b/db/finding_quality_test.goindex e29ec9b..d6ccc80 100644--- a/db/finding_quality_test.go+++ b/db/finding_quality_test.go@@ -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)}diff --git a/db/finding_reasoning_test.go b/db/finding_reasoning_test.goindex 5f63d07..f84e638 100644--- a/db/finding_reasoning_test.go+++ b/db/finding_reasoning_test.go@@ -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: {"", ""},diff --git a/db/finding_traffic_archive_test.go b/db/finding_traffic_archive_test.goindex 2460407..276f1f5 100644--- a/db/finding_traffic_archive_test.go+++ b/db/finding_traffic_archive_test.go@@ -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)diff --git a/db/findings_test.go b/db/findings_test.goindex d8fea5e..4b36ca0 100644--- a/db/findings_test.go+++ b/db/findings_test.go@@ -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 {@@ -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)@@ -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 {@@ -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)diff --git a/db/lessons_test.go b/db/lessons_test.goindex 2044551..d145c8a 100644--- a/db/lessons_test.go+++ b/db/lessons_test.go@@ -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)@@ -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 {@@ -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)@@ -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 {@@ -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)@@ -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)@@ -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)diff --git a/db/llm_usage_cost_test.go b/db/llm_usage_cost_test.goindex 0080c0b..d14233b 100644--- a/db/llm_usage_cost_test.go+++ b/db/llm_usage_cost_test.go@@ -44,7 +44,7 @@ func TestCostSeparatesFreshReadAndWriteBuckets(t *testing.T) {if err != nil {t.Fatal(err)}- defer d.Close()+ t.Cleanup(func() { d.Close() })priced, unpriced := costProfiles(t, d)// Every case adds 100k output tokens: 0.1 × 4.00 = 0.40.for _, tc := range []struct {@@ -82,7 +82,7 @@ func TestUsageByProfileCostAggregatesRows(t *testing.T) {if err != nil {t.Fatal(err)}- defer d.Close()+ t.Cleanup(func() { d.Close() })priced, unpriced := costProfiles(t, d)t.Cleanup(func() { d.Exec(`DELETE FROM llm_usage WHERE profile_name IN ($1,$2)`, priced, unpriced) })rows := []LLMUsage{diff --git a/db/notification_test.go b/db/notification_test.goindex 40f7bfd..fe7e2b4 100644--- a/db/notification_test.go+++ b/db/notification_test.go@@ -534,7 +534,7 @@ func TestSetFindingStatusWithNotifyOnlyEmitsOnRealChange(t *testing.T) {if err != nil {t.Fatal(err)}- defer d.DeleteTask(tk.ID)+ t.Cleanup(func() { _ = d.DeleteTask(tk.ID) })es := d.Exploration(tk.ExplorationID)f, err := es.RecordFinding(ctx, RecordFindingInput{TaskID: tk.ID, Worker: "test", VulnClass: "SQL injection", Name: "status change case",@@ -543,7 +543,10 @@ func TestSetFindingStatusWithNotifyOnlyEmitsOnRealChange(t *testing.T) {if err != nil {t.Fatal(err)}- t.Cleanup(func() { d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, f.FindingID) })+ t.Cleanup(func() {+ _, _ = d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, f.FindingID)+ _, _ = d.DeleteFinding(f.FindingID)+ })// Writing the finding already registered one finding_created event; count it as the baseline.var base int@@ -617,7 +620,7 @@ func TestSetFindingStatusesWithNotify(t *testing.T) {if err != nil {t.Fatal(err)}- defer d.DeleteTask(tk.ID)+ t.Cleanup(func() { _ = d.DeleteTask(tk.ID) })es := d.Exploration(tk.ExplorationID)record := func(name string) int64 {fi, err := es.RecordFinding(ctx, RecordFindingInput{@@ -627,7 +630,10 @@ func TestSetFindingStatusesWithNotify(t *testing.T) {if err != nil {t.Fatal(err)}- t.Cleanup(func() { d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, fi.FindingID) })+ t.Cleanup(func() {+ _, _ = d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, fi.FindingID)+ _, _ = d.DeleteFinding(fi.FindingID)+ })return fi.FindingID}first, second := record("bulk one"), record("bulk two")@@ -891,7 +897,7 @@ func TestFinishFindingRetestEmitsStatusChange(t *testing.T) {if err != nil {t.Fatal(err)}- defer d.DeleteTask(tk.ID)+ t.Cleanup(func() { _ = d.DeleteTask(tk.ID) })es := d.Exploration(tk.ExplorationID)f, err := es.RecordFinding(ctx, RecordFindingInput{TaskID: tk.ID, Worker: "test", VulnClass: "SQL injection", Name: "retest target",@@ -900,7 +906,10 @@ func TestFinishFindingRetestEmitsStatusChange(t *testing.T) {if err != nil {t.Fatal(err)}- t.Cleanup(func() { d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, f.FindingID) })+ t.Cleanup(func() {+ _, _ = d.Exec(`DELETE FROM notification_events WHERE finding_id=$1`, f.FindingID)+ _, _ = d.DeleteFinding(f.FindingID)+ })// Create a retest record and drive it straight to completion.rt, _, _, err := d.CreateFindingRetest(ctx, f.FindingID, "review")@@ -910,6 +919,8 @@ func TestFinishFindingRetestEmitsStatusChange(t *testing.T) {if rt.ConversationID == nil {t.Fatal("a retest should be associated with a session")}+ retestConvID := *rt.ConversationID+ t.Cleanup(func() { _ = d.DeleteConversation(retestConvID) })// A retest must enter running before a verdict can land (consistent with the real flow).if ok, err := d.StartFindingRetest(ctx, rt.ID); err != nil || !ok {t.Fatalf("failed to start retest: ok=%v err=%v", ok, err)diff --git a/db/task_archives_test.go b/db/task_archives_test.goindex 727ffc7..51986f9 100644--- a/db/task_archives_test.go+++ b/db/task_archives_test.go@@ -154,19 +154,28 @@ func TestTaskArchiveDatabaseRoundTrip(t *testing.T) {VALUES($1,$2,0,'quota_exhausted','balance exhausted',$3,$4,$3)`, task.ID, llmProfileID, exhaustedAt, chainCreatedAt); err != nil {t.Fatal(err)}+ var assetID, findingID int64defer func() {_, _ = d.Exec(`DELETE FROM llm_usage WHERE task_id=$1 OR exploration_id=$2`, fmt.Sprint(task.ID), task.ExplorationID)+ _, _ = d.Exec(`DELETE FROM llm_records WHERE task_id=$1`, fmt.Sprint(task.ID))_, _ = d.Exec(`DELETE FROM skill_usage WHERE task_id=$1 OR exploration_id=$2`, task.ID, task.ExplorationID)_, _ = d.Exec(`DELETE FROM tool_usage WHERE task_id=$1 OR exploration_id=$2`, task.ID, task.ExplorationID)+ _, _ = d.DeleteFindingsByTask(task.ID)+ if findingID != 0 {+ _, _ = d.DeleteFinding(findingID)+ }_, _ = d.Exec(`DELETE FROM task_archives WHERE task_id=$1`, task.ID)_ = d.DeleteTask(task.ID)+ if assetID != 0 {+ _, _ = d.Exec(`DELETE FROM assets WHERE id=$1`, assetID)+ }_, _ = d.Exec(`DELETE FROM llm_profiles WHERE id=$1`, llmProfileID)_, _ = d.Exec(`DELETE FROM companies WHERE id=$1`, companyID)}()if err := d.SetPaused(task.ID, true); err != nil {t.Fatal(err)}- assetID, err := d.Assets().UpsertRootDomain(UpsertRootDomainReq{Domain: fmt.Sprintf("archive-%d.example", task.ID), TaskID: task.ID})+ assetID, err = d.Assets().UpsertRootDomain(UpsertRootDomainReq{Domain: fmt.Sprintf("archive-%d.example", task.ID), TaskID: task.ID})if err != nil {t.Fatal(err)}@@ -197,7 +206,7 @@ VALUES($1,$2,0,'quota_exhausted','balance exhausted',$3,$4,$3)`, task.ID, llmPro}); err != nil {t.Fatal(err)}- if _, err := d.AddFinding(task.ID, nodeID, vulnclass, "archive finding", SeverityCritical, "summary", "evidence", "worker", []int64{assetID}); err != nil {+ if findingID, err = d.AddFinding(task.ID, nodeID, vulnclass, "archive finding", SeverityCritical, "summary", "evidence", "worker", []int64{assetID}); err != nil {t.Fatal(err)}archive, err := d.QueueTaskArchive(task.ID)diff --git a/db/task_assets_test.go b/db/task_assets_test.goindex 479cc98..78d019f 100644--- a/db/task_assets_test.go+++ b/db/task_assets_test.go@@ -168,8 +168,9 @@ func TestIntentAssetsIncludesDirectSourceProvenance(t *testing.T) {cleanupErr(t, "source task", d.DeleteTask(source.ID))})+ intentHost := fmt.Sprintf("intent-%d.example.test", time.Now().UnixNano())assetID, err := d.Assets().UpsertHTTPService(UpsertHTTPServiceReq{- URL: fmt.Sprintf("https://intent-%d.example.test", time.Now().UnixNano()), TaskID: source.ID,+ URL: "https://" + intentHost, TaskID: source.ID,})if err != nil {t.Fatal(err)@@ -177,6 +178,9 @@ func TestIntentAssetsIncludesDirectSourceProvenance(t *testing.T) {t.Cleanup(func() {_, err := d.Assets().DeleteByIDs([]int64{assetID})cleanupErr(t, "asset", err)+ // UpsertHTTPService also creates the parent subdomain and root_domain rows.+ _, err = d.Exec(`DELETE FROM assets WHERE domain IN ($1, 'example.test')`, intentHost)+ cleanupErr(t, "parent assets", err)})intentID, err := d.Exploration(source.ExplorationID).AddIntent(map[string]any{"summary": "source worker"}, 5, []int64{assetID}, "planner")if err != nil {diff --git a/server/chat_llm_resolve_test.go b/server/chat_llm_resolve_test.goindex 19539a6..2a713b9 100644--- a/server/chat_llm_resolve_test.go+++ b/server/chat_llm_resolve_test.go@@ -16,7 +16,7 @@ func TestChatUnavailableReasonDistinguishesStates(t *testing.T) {if err != nil {db.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })td := t.TempDir()ctx, cancel := context.WithCancel(context.Background())s := New(ctx, m, td, td, td)@@ -68,7 +68,7 @@ func TestResolveChatAgentHonoursConversationProfile(t *testing.T) {if err != nil {db.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })td := t.TempDir()ctx, cancel := context.WithCancel(context.Background())s := New(ctx, m, td, td, td)diff --git a/server/engine_budget_test.go b/server/engine_budget_test.goindex 64eb2b9..bd986cb 100644--- a/server/engine_budget_test.go+++ b/server/engine_budget_test.go@@ -84,7 +84,7 @@ func TestResetSettledRunReseedsBudgetFromUsage(t *testing.T) {if err != nil {pgdb.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })taskID := strconv.FormatInt(time.Now().UnixNano(), 10)t.Cleanup(func() {diff --git a/server/finding_traffic_test.go b/server/finding_traffic_test.goindex 4525991..001024b 100644--- a/server/finding_traffic_test.go+++ b/server/finding_traffic_test.go@@ -38,7 +38,11 @@ func trafficEvidenceServer(t *testing.T) (*Server, *db.RecordedFinding, func(strt.Fatal(err)}tid, _ := strconv.ParseInt(task.ID, 10, 64)- t.Cleanup(func() { m.pg.Exec(`DELETE FROM task_archives WHERE task_id=$1`, tid); m.pg.DeleteTask(tid) })+ t.Cleanup(func() {+ _, _ = m.pg.DeleteFindingsByTask(tid)+ _, _ = m.pg.Exec(`DELETE FROM task_archives WHERE task_id=$1`, tid)+ _ = m.pg.DeleteTask(tid)+ })ctx, cancel := context.WithCancel(context.Background())cancel()s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())diff --git a/server/lessons_test.go b/server/lessons_test.goindex 6ab6052..3337f17 100644--- a/server/lessons_test.go+++ b/server/lessons_test.go@@ -36,7 +36,10 @@ func TestTaskLessonsExtractionAndIdempotency(t *testing.T) {t.Fatal(err)}taskID, _ := strconv.ParseInt(task.ID, 10, 64)- defer func() { _ = m.pg.DeleteTask(taskID) }()+ defer func() {+ _, _ = m.pg.DeleteFindingsByTask(taskID)+ _ = m.pg.DeleteTask(taskID)+ }()defer func() { _, _ = m.pg.Exec(`DELETE FROM rsi_lessons`) }()if _, err := m.pg.Exec(`@@ -138,7 +141,10 @@ func TestTerminalStatusExtractsLessons(t *testing.T) {t.Fatal(err)}taskID, _ := strconv.ParseInt(task.ID, 10, 64)- defer func() { _ = m.pg.DeleteTask(taskID) }()+ defer func() {+ _, _ = m.pg.DeleteFindingsByTask(taskID)+ _ = m.pg.DeleteTask(taskID)+ }()if _, err := m.pg.Exec(`INSERT INTO findings(task_id,vulnclass,name,summary,severity,status)VALUES ($1,'xss','Reflected XSS','reflected in search','high','confirmed')`, taskID); err != nil {diff --git a/server/manager_delete_files_test.go b/server/manager_delete_files_test.goindex 93b4d1a..6188f5b 100644--- a/server/manager_delete_files_test.go+++ b/server/manager_delete_files_test.go@@ -18,7 +18,7 @@ func TestSeedAssociatesTargetAssetWithTask(t *testing.T) {if err != nil {pgdb.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })task, err := m.CreateTask("seed ownership", "seed ownership", nil, 0, 0)if err != nil {t.Fatal(err)diff --git a/server/notify_api_test.go b/server/notify_api_test.goindex b52757f..f98d17a 100644--- a/server/notify_api_test.go+++ b/server/notify_api_test.go@@ -72,7 +72,10 @@ func newNotifyFixture(t *testing.T) *notifyFixture {if err != nil {t.Fatal(err)}- t.Cleanup(func() { pg.Exec(`DELETE FROM tasks WHERE id=$1`, taskID) })+ t.Cleanup(func() {+ _, _ = pg.DeleteFindingsByTask(taskID)+ _ = pg.DeleteTask(taskID)+ })var mark int64if err := pg.QueryRow(`SELECT COALESCE(max(id),0) FROM notification_events`).Scan(&mark); err != nil {diff --git a/server/planner_quiet_test.go b/server/planner_quiet_test.goindex 8109ed2..8d9ec30 100644--- a/server/planner_quiet_test.go+++ b/server/planner_quiet_test.go@@ -24,6 +24,7 @@ func TestPlannerQuietSkipEdge(t *testing.T) {if err != nil {t.Fatal(err)}+ t.Cleanup(func() { _, _ = m.DeleteTask(created.ID, DeleteTaskOptions{}) })tt, ok := m.Task(created.ID)if !ok {t.Fatal("task not found after creation")@@ -93,6 +94,7 @@ func TestPlannerQuietModeToggleMidRun(t *testing.T) {if err != nil {t.Fatal(err)}+ t.Cleanup(func() { _, _ = m.DeleteTask(created.ID, DeleteTaskOptions{}) })tt, ok := m.Task(created.ID)if !ok {t.Fatal("task not found")diff --git a/server/record_fact_rawinput_test.go b/server/record_fact_rawinput_test.goindex 92a643a..b61b440 100644--- a/server/record_fact_rawinput_test.go+++ b/server/record_fact_rawinput_test.go@@ -73,7 +73,7 @@ func TestFix410AcceptsRawInputSurvivesWrappers(t *testing.T) {if err != nil {db.SkipPGUnavailable(t, err)}- defer pg.Close()+ t.Cleanup(func() { _ = pg.Close() })agent.SetToolAugment(nil)wireTools(pg, nil)task, err := pg.CreateTaskWithOptions("fix410", "record_fact through the wrapper chain", db.TaskCreateOptions{})diff --git a/server/target_gate_scope_test.go b/server/target_gate_scope_test.goindex 2f4b1dc..bd42b93 100644--- a/server/target_gate_scope_test.go+++ b/server/target_gate_scope_test.go@@ -174,6 +174,7 @@ func (f *targetScopeFixture) newTask(t *testing.T) *Task {if err != nil {t.Fatal(err)}+ t.Cleanup(func() { _, _ = f.m.DeleteTask(created.ID, DeleteTaskOptions{}) })task, ok := f.m.Task(created.ID)if !ok {t.Fatal("task not found after creation")diff --git a/server/task_admission_test.go b/server/task_admission_test.goindex 7d7b628..b43e795 100644--- a/server/task_admission_test.go+++ b/server/task_admission_test.go@@ -650,6 +650,15 @@ func TestTaskLLMResolutionReportsDatabaseFailure(t *testing.T) {if err != nil {t.Fatal(err)}+ taskID := mustTaskID(t, task.ID)+ t.Cleanup(func() {+ if dsn, _, err := db.DSN(); err == nil {+ if pg, err := db.Open(dsn); err == nil {+ _ = pg.DeleteTask(taskID)+ _ = pg.Close()+ }+ }+ })if err := m.pg.Close(); err != nil {t.Fatal(err)}diff --git a/server/task_credentials_test.go b/server/task_credentials_test.goindex 5376ac3..eacbdf9 100644--- a/server/task_credentials_test.go+++ b/server/task_credentials_test.go@@ -49,6 +49,7 @@ func TestTaskCredentialsFlow(t *testing.T) {if err != nil {t.Fatal(err)}+ t.Cleanup(func() { _ = s.m.pg.DeleteTask(id) })t.Cleanup(func() { s.m.PG().Exec(`DELETE FROM tasks WHERE id=$1`, id) })// List: both rows, user source, order stable.diff --git a/server/task_llm_test.go b/server/task_llm_test.goindex 60324f4..15c1c33 100644--- a/server/task_llm_test.go+++ b/server/task_llm_test.go@@ -656,7 +656,7 @@ func TestProfileDeleteRestoresQuotaBlockedIntentWhenFallbackAvailable(t *testingif err != nil {db.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })profileID, err := m.pg.SaveProfile(&db.LLMProfile{Name: fmt.Sprintf("delete-quota-fallback-%d", time.Now().UnixNano()),diff --git a/server/task_outcome_test.go b/server/task_outcome_test.goindex 3d2b56e..c1f5717 100644--- a/server/task_outcome_test.go+++ b/server/task_outcome_test.go@@ -22,7 +22,10 @@ func TestTaskOutcomeCard(t *testing.T) {t.Fatal(err)}taskID, _ := strconv.ParseInt(task.ID, 10, 64)- defer func() { _ = m.pg.DeleteTask(taskID) }()+ defer func() {+ _, _ = m.pg.DeleteFindingsByTask(taskID)+ _ = m.pg.DeleteTask(taskID)+ }()// skill_usage has no foreign keys, so DeleteTask leaves these rows behind,// and their misses would outrank TestSkillGapsReport's fixture in later runs.defer func() { _, _ = m.pg.Exec(`DELETE FROM skill_usage WHERE task_id=$1`, taskID) }()diff --git a/server/task_templates_test.go b/server/task_templates_test.goindex 63b6c04..9b1c5c3 100644--- a/server/task_templates_test.go+++ b/server/task_templates_test.go@@ -91,7 +91,7 @@ func TestConversationPatchReturnsPinState(t *testing.T) {if err != nil {db.SkipPGUnavailable(t, err)}- defer m.Close()+ t.Cleanup(func() { _ = m.Close() })ctx, cancel := context.WithCancel(context.Background())s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())defer stopTestServer(t, s, cancel)diff --git a/server/traffic_rewrite_list_test.go b/server/traffic_rewrite_list_test.goindex 7b49e8f..e0103f9 100644--- a/server/traffic_rewrite_list_test.go+++ b/server/traffic_rewrite_list_test.go@@ -269,6 +269,7 @@ VALUES(?,?, 'HTTP/1.1 200 OK', ?, ?)`, id, "GET "+path+" HTTP/1.1\nHost: "+host,}tidV1, _ := strconv.ParseInt(taskV1.ID, 10, 64)t.Cleanup(func() {+ _, _ = s.m.pg.DeleteFindingsByTask(tidV1)_, _ = s.m.pg.Exec(`DELETE FROM task_archives WHERE task_id=$1`, tidV1)_ = s.m.pg.DeleteTask(tidV1)})