Skip to content

refactor(deadline): the pieces of an overall deadline for classified traces, unused so far - #199

Open
dweekly wants to merge 5 commits into
mainfrom
refactor/0.19-deadline-prep
Open

dweekly wants to merge 5 commits into
mainfrom
refactor/0.19-deadline-prep

Conversation

@dweekly

@dweekly dweekly commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Summary

This is preparation for Deadline For Traces, the 0.19.0 slice that gives traceClassified one overall deadline. It adds the pieces that deadline will be built from and tests each one on its own. Nothing a caller can see changes. No production path calls the new code yet, and the two existing functions it touches (TraceClassifier.classify and askWithinDeadline) keep exactly their current behaviour.

The pieces:

  • TraceDeadline (new file TraceDeadline.swift): one absolute instant on DiscoveryClock, the clock that counts system sleep. For a phase with its own cap, it gives the earlier of the two cutoffs; a tie goes to the overall deadline. It also arms the wake-up for that instant.

  • raceDeadline(until:arm:_:): runs work against a deadline without waiting for the work to finish.

    • A deadline already passed starts nothing.
    • A value or error that arrives after the instant counts as expiry.
    • The caller's cancellation wins over expiry, but not over a value that arrived in time.
    • When either side wins, the work is cancelled and the wake-up is stopped.

    DeadlineRace, the existing race behind askWithinDeadline, is now generic over the value and the clock. askWithinDeadline uses it with its old clock and the same results.

  • RDNSCache.batchLookup(_:until:): ends the wait for a reverse-DNS batch at the deadline and returns every answer that arrived before the instant.

    • Those answers are kept by the batch itself, so a cache of size 0 or with a TTL of 0 still returns them.
    • After the instant, no query is started or joined, and only answers already cached are returned.
    • Queries the batch leaves behind run on to their own timeouts, and each outcome is applied once.
    • A nil deadline is exactly batchLookup(_:).
  • TraceClassifier split: classify is now asnLookupAddresses (which addresses go to the ASN resolver) followed by categorize (categories and gap filling from the answers in hand), with the same output.

  • TestSeams.observeBatchAnswer: lets a test move the clock only once an answer is in.

Behaviour changes

None. Evidence:

  • Classifier. ClassifierSplitTests was committed first (ed77c15). It records classify's output on unmodified main for six inputs: a filled gap, a resolver that throws (the try? path), non-global hops after a public one, a VPN trace, IPv6 with link-local, and a trace with no answers. It also records which addresses and timeouts reached the resolver. After the split, both classify and the hand-composed parts reproduce those records exactly.
  • Existing suites. They pass unchanged; no existing test was edited.
  • API. The API check against v0.18.1 reports nothing new.

Reproduce

swift format lint --strict -r Sources Tests
SKIP_NETWORK_TESTS=1 PTR_SKIP_STUN=1 swift test -c debug --filter 'DeadlinePrepTests|ClassifierSplitTests'
SKIP_NETWORK_TESTS=1 PTR_SKIP_STUN=1 swift test -c debug
swift package diagnose-api-breaking-changes v0.18.1 --products SwiftFTR \
  --breakage-allowlist-path "$PWD/api-breakage-allowlist.txt"
# The recorded classifier output passes on main's code: the recording commit adds only the test.
git checkout ed77c15 && SKIP_NETWORK_TESTS=1 PTR_SKIP_STUN=1 swift test -c debug --filter ClassifierSplitTests

Mutations

Each mutation below was applied by itself, the two new suites were run, and the file was restored. Every one makes the named test fail.

Mutation Fails
raceDeadline starts work after the deadline has passed "A deadline already passed starts no work and arms no wake-up"
A value committed after the deadline is accepted "A value or an error committed after the deadline is expiry"
The work is not cancelled when the race is decided "At the deadline the caller is released while the work still runs, and the work is cancelled"
The wake-up is not stopped "A value in time is returned, and the wake-up is stopped" (and others)
The caller's cancellation is ignored "The caller's cancellation releases it at once…" (hits the 20 s watchdog)
A tie between cap and deadline goes to the cap "A deadline's cutoff is the earlier instant…"
lookup starts or joins a query after the deadline "Past the deadline a lookup starts no query and joins none…" (watchdog)
The batch does not keep its names "At the deadline a batch returns the answers it has…", and the no-deadline comparison
Shared queries are cancelled when a batch is cut "A query a batch left at its deadline runs on, and its answer is applied once"
A nil deadline is not batchLookup(_:) "Without a deadline … a batch gives today's result"
try? dropped from classify "classify gives the recorded output" (the throwing-resolver case)
Gap filling removed both recorded-output tests
The public address is not sent to the ASN resolver both recorded-output tests

Gates

swift format lint --strict -r Sources Tests                      exit 0, no output
swift test -c debug (offline), run 1                             exit 0; 684 tests, 11 known issues; XCTest 126, 0 failures; "recorded an issue": 0
swift test -c debug (offline), run 2                             same
swift test -c release (offline)                                  same
30 whole runs of the touched suites, plain                       30/30
30 whole runs, LIBDISPATCH_COOPERATIVE_POOL_STRICT=1             30/30
diagnose-api-breaking-changes v0.18.1 (allowlist)                "No breaking changes detected in SwiftFTR"
DocC --warnings-as-errors                                        exit 0
orphan-audit                                                     clean: 44 tracked .md

The touched suites are DeadlinePrepTests, ClassifierSplitTests, the RDNSCache, RDNSHoldoff, RDNSSharedLookup and RDNSSuppression tests, PublicIPResolverTests, and the three classification suites.

🤖 Generated with Claude Code

https://claude.ai/code/session_01A8mvWou2vH56zDnkZkqT2X

dweekly and others added 5 commits November 27, 2026 16:11
Recorded from TraceClassifier.classify on main before it is split into its
ASN lookup and its categorization, so the split can be held to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A8mvWou2vH56zDnkZkqT2X
- TraceDeadline: one absolute instant on DiscoveryClock, with the cutoff for
  a phase cap (a tie is the deadline's) and its wake-up.
- raceDeadline: work raced against a deadline, not joined; a passed deadline
  starts nothing; a late value or error is expiry; the caller's cancellation
  wins over expiry. DeadlineRace is generic over the value and the clock, and
  askWithinDeadline uses it unchanged.
- RDNSCache.batchLookup(_:until:): answers gathered before the instant, kept
  apart from the cache; no query started or joined after it; nil is
  batchLookup(_:) exactly.
- TraceClassifier.classify split into asnLookupAddresses and categorize, with
  the same output.

Preparation for Deadline For Traces; no caller sees a change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A8mvWou2vH56zDnkZkqT2X
The race resumes its caller before it stops the wake-up, so reading the stop
count at once could see zero on a correct run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A8mvWou2vH56zDnkZkqT2X
…olds

When the instant passed between the batch's first check and its race, the
race expired without running a lookup and the batch returned nothing, even
for an address already cached. At expiry the batch now fills any address it
has no answer for from the cache, without asking.

Shown by cachedAnswerSurvivesExpiryDuringSetup, which fails without the
cache read.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A8mvWou2vH56zDnkZkqT2X

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant