ARTEX/agent/triage_review_test.go
Николай Гусев a3e44d3f05 agent: close the review blockers on the advisory duplicate check
Fixes the three blockers and three shoulds from the review of #470, rebased
onto current main.

Blocker 1 — no way out of a wrong suppression. The suppression note told the
agent to register a distinct defect again, but report_finding had no override:
the retry hit the same check and was suppressed again. report_finding now takes
force_register, which skips the duplicate check for that call and logs the
override. Covered end to end: suppress, then register with the flag.

Blocker 2 — id mismatch. The suppression answer returned duplicate_of_finding_id
(the findings-table id) while update_finding_report addresses the exploration
NODE. The answer now carries duplicate_of_finding_node_id as well, and the note
names the node id to pass. withNodeID resolves it from the candidates the
question offered, so an id the model was never shown maps to nothing.

Blocker 3 — unguarded HTTP client. SystemOneClient.http() returned a bare client,
so a stored triage profile could point the server at an internal host. The client
is now built by the server under the same endpoint guard as the model-list path:
ARTEX_LLM_PRIVATE_ENDPOINTS allow-list, private and metadata addresses refused,
no proxy, redirects not followed. A refused base_url leaves triage off (fail
open, as every other triage failure). TriageHTTPTimeout is exported so the
injected client uses the same budget instead of a second number.

Should 4 — UTF-8 truncation. capText and buildDuplicateState cut by byte index,
splitting a multi-byte rune on CJK or Cyrillic summaries. Both now cut on a rune
boundary.

Should 5 — settings validation. putSettings accepts and stores any
triage_profile_id and any confidence. Both are validated before anything is
written: a negative or unknown profile id and a threshold outside 0..1 are
rejected with 400; 0 still turns the check off.

Should 6 — the threshold is a setting (triage_min_confidence), read on boot and
on change; the description matches the code.

Verification: go build ./... and go vet clean; gofmt clean. go test ./agent/
./db/ ./llmrec/ ./server/ with ARTEX_PG_DSN on a local PostgreSQL — green except
TestManagerSOCKS5UDPOptInAndAssetBlockRuleDropsDatagram (binds 127.0.0.2) and
TestPauseShiftsTaskDeadlineOnResume, which fail the same way on clean main on
macOS. Red/green: with the force_register branch disabled the override test
fails naming the ignored flag; restored, it passes.
2026-11-23 17:22:16 +03:00

92 lines
3.4 KiB
Go

package agent
import (
"strings"
"testing"
"unicode/utf8"
"github.com/Autumn-27/artex/db"
)
// The caps bound a request in BYTES, but a byte-index cut through a multi-byte
// rune leaves a broken character: the JSON encoder sends U+FFFD and the decider
// reads garbage where the evidence tail was. Every cut must land on a boundary.
func TestCapTextCutsOnRuneBoundary(t *testing.T) {
for _, tc := range []struct {
name string
text string
cap int
}{
{"cyrillic", strings.Repeat("уязвимость ", 80), 51},
{"cjk", strings.Repeat("数据泄露", 100), 401},
{"mixed", "SQLi: " + strings.Repeat("инъекция ", 90), 333},
} {
t.Run(tc.name, func(t *testing.T) {
out := capText(tc.text, tc.cap)
if !utf8.ValidString(out) {
t.Fatalf("capText(%d) produced invalid UTF-8", tc.cap)
}
if !strings.HasSuffix(out, "…") {
t.Fatalf("capText(%d) dropped the ellipsis", tc.cap)
}
body := strings.TrimSuffix(out, "…")
if len(body) > tc.cap {
t.Fatalf("capText(%d) kept %d bytes", tc.cap, len(body))
}
if !strings.HasPrefix(tc.text, body) {
t.Fatalf("capText(%d) is not a prefix of the input", tc.cap)
}
})
}
}
// The assembled state has its own byte ceiling; the same rule applies there, and
// the whole state must still parse as text.
func TestBuildDuplicateStateCutsOnRuneBoundary(t *testing.T) {
existing := make([]TriageCandidate, 0, 40)
for i := 0; i < 40; i++ {
existing = append(existing, TriageCandidate{
FindingID: int64(i + 1),
Name: "数据泄露",
Summary: strings.Repeat("утечка данных через параметр ", 20),
})
}
state := buildDuplicateState(TriageCandidate{Name: "SQLi", Summary: strings.Repeat("данные ", 200)}, existing)
if len(state) > triageMaxStateChars {
t.Fatalf("state is %d bytes, cap is %d", len(state), triageMaxStateChars)
}
if !utf8.ValidString(state) {
t.Fatal("state was cut mid-rune")
}
}
// The question and the suppression answer carry the findings-table id, while
// update_finding_report addresses the exploration node — the candidate list has
// to keep both so the answer can name the one the agent needs to act on.
func TestTriageCandidatesCarryNodeID(t *testing.T) {
nodes := []*db.Node{
{ID: 41, FindingID: 7, Payload: []byte(`{"name":"SQLi","vulnclass":"sqli","severity":"high","summary":"s"}`)},
{ID: 42, FindingID: 0, Payload: []byte(`{"name":"no finding row"}`)},
}
out := triageCandidates(nodes, db.RecordFindingInput{})
if len(out) != 1 {
t.Fatalf("candidates = %d, want 1 (node without a finding row is skipped)", len(out))
}
if out[0].NodeID != 41 || out[0].FindingID != 7 {
t.Fatalf("candidate ids = node %d / finding %d, want 41 / 7", out[0].NodeID, out[0].FindingID)
}
}
func TestWithNodeIDMapsFindingToNode(t *testing.T) {
existing := []TriageCandidate{{FindingID: 7, NodeID: 41}, {FindingID: 9, NodeID: 55}}
if got := withNodeID(DuplicateAnswer{DuplicateOf: 9, Confidence: 0.8}, existing); got.DuplicateOfNode != 55 {
t.Fatalf("DuplicateOfNode = %d, want 55", got.DuplicateOfNode)
}
if got := withNodeID(DuplicateAnswer{}, existing); got.DuplicateOfNode != 0 {
t.Fatalf("distinct answer carried node %d", got.DuplicateOfNode)
}
// An id the question never offered must not invent a node.
if got := withNodeID(DuplicateAnswer{DuplicateOf: 999}, existing); got.DuplicateOfNode != 0 {
t.Fatalf("unoffered id mapped to node %d", got.DuplicateOfNode)
}
}