mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 09:20:53 +08:00
prove_goal's finding proof was meant to confirm that each bound traffic_ref still exists in the live capture, but SetTrafficProofChecker was only called from tests: Planner.Plan built its ToolSet without it, exchangeExists was nil, and VerifyFindingProofForNode skipped the check silently. Decision: wire it (option a). The capture store is reachable when Plan() runs: server/task_llm.go (agentsForTask) already gives the planner SetFindingRecorder(s.evidenceStore()), and that evidence store is built on s.m.traffic, the live capture. So: - traffic.Traffic.ExchangeExists looks the id up in the capture index (nil store or lookup failure is an error). - evidence.Store.TrafficExists delegates to it. - agent.TrafficCaptureChecker is the interface; Plan() wires the recorder's TrafficExists into the ToolSet when the recorder implements it, so the production planner gets the check with no extra server wiring. - VerifyFindingProofForNode treats a nil checker as unavailable and returns an error, so prove_goal answers "refused (fail-closed)" instead of accepting the proof; a lookup error does the same. The capture store is global, not partitioned per task: the check proves the exchange is in this server's capture, and the host match against the finding's assets ties it to the target. Tests: adversarial bench case TestAdversarialBenchTrafficRefMissingFromCaptureRefusedPG builds the planner like agentsForTask (NewPlanner + SetFindingRecorder(evidence store)), with nothing injected into the ToolSet, and checks that a traffic_ref absent from the capture is refused, that a capture-less evidence store and a planner without a recorder refuse (fail-closed), and that a captured matching ref still closes the goal. The three refusal subtests fail on 5ba19cd. Existing ToolSet-level tests now set an explicit checker, and the existing bench round 2 binds an exchange that is in the capture and runs with the production recorder.
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package traffic
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// EvidenceExchange exposes complete captured bodies only during the callback.
|
|
// Readers are closed before releasing the writer lock, so host deletion cannot
|
|
// remove a blob between its index lookup and its evidence copy.
|
|
type EvidenceExchange struct {
|
|
ID, URL, Method, ContentType string
|
|
TS int64
|
|
Status int
|
|
ReqHead, RespHead string
|
|
ReqLen, RespLen int64
|
|
ReqHash, RespHash string
|
|
Request, Response io.Reader
|
|
}
|
|
|
|
// ExchangeExists reports whether id is an exchange in this capture store. A nil
|
|
// store or a failed lookup is an error, never a silent false or true.
|
|
func (t *Traffic) ExchangeExists(ctx context.Context, id string) (bool, error) {
|
|
if t == nil {
|
|
return false, errors.New("traffic recording store unavailable")
|
|
}
|
|
var one int
|
|
err := t.db.QueryRowContext(ctx, `SELECT 1 FROM exchanges WHERE id=?`, id).Scan(&one)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (t *Traffic) ReadEvidence(ctx context.Context, ids []string, consume func(EvidenceExchange) error) error {
|
|
if t == nil {
|
|
return errors.New("traffic recording store unavailable")
|
|
}
|
|
t.wmu.Lock()
|
|
defer t.wmu.Unlock()
|
|
for _, id := range ids {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if err := t.readEvidence(id, consume); err != nil {
|
|
return fmt.Errorf("traffic %s: %w", id, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *Traffic) readEvidence(id string, consume func(EvidenceExchange) error) error {
|
|
e := EvidenceExchange{ID: id}
|
|
var legacy string
|
|
if err := t.db.QueryRow(`SELECT ts,url,method,status,content_type,req_len,resp_len,path FROM exchanges WHERE id=?`, id).
|
|
Scan(&e.TS, &e.URL, &e.Method, &e.Status, &e.ContentType, &e.ReqLen, &e.RespLen, &legacy); err != nil {
|
|
return err
|
|
}
|
|
var req, resp []byte
|
|
var reqBlob, respBlob sql.NullString
|
|
err := t.db.QueryRow(`SELECT req_head,req_body,req_blob,resp_head,resp_body,resp_blob FROM exchange_bodies WHERE id=?`, id).
|
|
Scan(&e.ReqHead, &req, &reqBlob, &e.RespHead, &resp, &respBlob)
|
|
if errors.Is(err, sql.ErrNoRows) && legacy != "" {
|
|
if !filepath.IsLocal(legacy) {
|
|
return errors.New("invalid legacy traffic path")
|
|
}
|
|
r, err := os.Open(filepath.Join(t.dir, legacy, "request.http"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
p, err := os.Open(filepath.Join(t.dir, legacy, "response.http"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer p.Close()
|
|
e.ReqHead, e.Request, err = splitLegacyEvidence(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
e.RespHead, e.Response, err = splitLegacyEvidence(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return consume(e)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
e.Request = bytes.NewReader(req)
|
|
e.Response = bytes.NewReader(resp)
|
|
if reqBlob.Valid && reqBlob.String != "" {
|
|
f, _, err := t.Blob(reqBlob.String)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
e.Request = f
|
|
e.ReqHash = reqBlob.String
|
|
}
|
|
if respBlob.Valid && respBlob.String != "" {
|
|
f, _, err := t.Blob(respBlob.String)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
e.Response = f
|
|
e.RespHash = respBlob.String
|
|
}
|
|
return consume(e)
|
|
}
|
|
|
|
func splitLegacyEvidence(r io.Reader) (string, io.Reader, error) {
|
|
b := bufio.NewReader(r)
|
|
var head strings.Builder
|
|
for {
|
|
line, err := b.ReadString('\n')
|
|
if line == "\n" || line == "\r\n" {
|
|
return head.String(), b, nil
|
|
}
|
|
head.WriteString(line)
|
|
if head.Len() > 1<<20 {
|
|
return "", nil, errors.New("legacy traffic header too large")
|
|
}
|
|
if errors.Is(err, io.EOF) {
|
|
return head.String(), b, nil
|
|
}
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
}
|
|
}
|