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>
This commit is contained in:
Gary Hansen
2026-07-07 21:42:06 +10:00
co-authored by Claude Fable 5
parent af15c9c2d4
commit d71c7fbef2
53 changed files with 6685 additions and 9366 deletions
+63 -9
View File
@@ -1,16 +1,67 @@
package traverse
// EventStage mirrors the :stage symbols reported by traverser.rb's
// report_progress: new/start/answer/resolve plus the fast-mode and
// multi-childset variants.
type EventStage int
const (
EventStart EventStage = iota
EventComplete
// StageNew fires when a referral node is created (before processing).
StageNew EventStage = iota
// StageStart fires when a referral is popped for processing.
StageStart
// StageNewReferralSet fires once per extra childset when more than one
// IP of a server produced children.
StageNewReferralSet
// StageNewFast fires instead of StageNew when fast mode already knows
// this referral will be completed from the memo.
StageNewFast
// StageResolve fires after a resolve subtree's statistics are folded
// into the referral (post-order, Ruby's :calc_resolve marker).
StageResolve
// StageAnswer fires after a referral's statistics are calculated
// (post-order, Ruby's :calc_answer marker).
StageAnswer
// StageAnswerFast fires when fast mode replaced the referral with an
// earlier completed one instead of processing it.
StageAnswerFast
)
func (s EventStage) String() string {
switch s {
case StageNew:
return "new"
case StageStart:
return "start"
case StageNewReferralSet:
return "new_referral_set"
case StageNewFast:
return "new_fast"
case StageResolve:
return "resolve"
case StageAnswer:
return "answer"
case StageAnswerFast:
return "answer_fast"
default:
return "unknown"
}
}
// TraversalEvent is one progress callback. RefID/Status/IsResolve are
// denormalised from Referral so renderers (CLI, web) need not walk the tree.
type TraversalEvent struct {
Stage EventStage
Result TraversalResult
Stage EventStage
Referral *Referral
RefID string
// Status summarises the referral's outcome so far (see
// Referral.OverallStatus); empty before any response arrives.
Status Status
// IsResolve is true for nodes inside a glue-resolution subtree.
IsResolve bool
// CompletedEarlier carries the refid of the earlier identical referral
// on fast-mode events (StageNewFast / StageAnswerFast).
CompletedEarlier string
}
type EventHandler func(TraversalEvent)
@@ -19,13 +70,16 @@ type TraverserHooks struct {
OnEvent EventHandler
}
func (h *TraverserHooks) emit(stage EventStage, result TraversalResult, isResolve bool) {
if h == nil || h.OnEvent == nil {
func (h *TraverserHooks) emit(stage EventStage, r *Referral, completedEarlier string) {
if h == nil || h.OnEvent == nil || r == nil {
return
}
h.OnEvent(TraversalEvent{
Stage: stage,
Result: result,
IsResolve: isResolve,
Stage: stage,
Referral: r,
RefID: r.RefID,
Status: r.OverallStatus(),
IsResolve: r.IsResolve(),
CompletedEarlier: completedEarlier,
})
}