Files
Gary HansenandClaude Fable 5 d71c7fbef2 feat: rework engine and CLI for dnstraverse parity
Port the traversal engine to the Ruby dnstraverse model so behaviour and
output match dns.squish.net:

- dns: single RD=0 query path (RD=1 only for upstream root discovery),
  per-run packet cache, EDNS0 512-fallback with warnings, UDP->TCP on
  truncation; fix --retries 0 and --root-server IP-literal handling;
  drop all hardcoded 127.0.0.1:53 resolvers
- traverse: hierarchical per-branch InfoCache, 7-step response
  classification with the full 10-status vocabulary, bailiwick
  partitioning, strictly-deeper lame-referral rule, refid grammar with
  .0 resolve subtrees and childset digits, per-IP branching at 1/n
  weight, cache-based glue resolution with noglue/loop dead ends, CNAME
  restarts from the deepest cached zone, fast-mode memoization,
  probability aggregation with Ruby-identical stats keys (sums to 1.0)
- output: byte-for-byte reference text format pinned by a golden test,
  reference CLI defaults, working --quiet/--show-X=false, TTY-aware
  colour, deduplicated deterministic JSON
- web: adapt API/SPA to the new engine, SSE events carry refid/status,
  fix subscribe/snapshot duplicate-event race and a statusCls TDZ bug,
  align SPA type list with the backend
- delete the old engine and dead code (net -4,350 lines)

Verified against live runs of the reference Ruby engine across five
domains (answers, NXDOMAIN, null MX, CNAME restart, glueless resolve)
with no divergences beyond the documented typo fixes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 21:42:06 +10:00

75 lines
2.1 KiB
Go

package traverse
import (
"sort"
"strings"
miekgdns "github.com/miekg/dns"
)
// AnswerStat is one distinct answered RRset with its accumulated probability.
type AnswerStat struct {
// Key groups identical RRset content: sorted rdata strings joined with
// "@@@" (summary_stats.rb get_answer_stats).
Key string
Prob float64
RRs []miekgdns.RR
}
// SummaryStats groups the aggregated leaves by status; answered leaves are
// additionally grouped by RRset content, one entry per distinct RRset
// (summary_stats.rb). The ByStatus probabilities sum to 1.0 and the answer
// probabilities sum to ByStatus[StatusAnswered].
type SummaryStats struct {
ByStatus map[Status]float64
Answers []AnswerStat
}
// SummaryStats computes (and memoises) the summary grouping of this node's
// aggregated leaf statistics (referral.rb summary_stats). It returns nil
// until the node's statistics have been calculated.
func (r *Referral) SummaryStats() *SummaryStats {
if r == nil || !r.calculated || len(r.Stats) == 0 {
return nil
}
if r.summaryStats != nil {
return r.summaryStats
}
stats := &SummaryStats{ByStatus: make(map[Status]float64)}
answers := make(map[string]*AnswerStat)
for _, leaf := range r.StatsList() {
status := leaf.Response.Status
stats.ByStatus[status] += leaf.Prob
if status != StatusAnswered {
continue
}
rdatas := make([]string, 0, len(leaf.Response.DQ.Answers))
for _, rr := range leaf.Response.DQ.Answers {
rdatas = append(rdatas, rrData(rr))
}
sort.Strings(rdatas)
key := strings.Join(rdatas, "@@@")
if e, ok := answers[key]; ok {
e.Prob += leaf.Prob
} else {
answers[key] = &AnswerStat{Key: key, Prob: leaf.Prob, RRs: leaf.Response.DQ.Answers}
}
}
for _, e := range answers {
stats.Answers = append(stats.Answers, *e)
}
sort.Slice(stats.Answers, func(i, j int) bool { return stats.Answers[i].Key < stats.Answers[j].Key })
r.summaryStats = stats
return stats
}
// rrData extracts the rdata portion of a record (dnsruby rdata_to_string):
// everything after owner/TTL/class/type in presentation format.
func rrData(rr miekgdns.RR) string {
s := rr.String()
h := rr.Header().String()
return strings.TrimPrefix(s, h)
}