diff --git a/internal/cli/permute_budget_test.go b/internal/cli/permute_budget_test.go new file mode 100644 index 0000000..89f271e --- /dev/null +++ b/internal/cli/permute_budget_test.go @@ -0,0 +1,54 @@ +package cli + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestE2EExpiredMainPassDefersPermutation(t *testing.T) { + t.Parallel() + words := writeFile(t, "words.txt", "www\nmail\nweb\n") + seeds := writeFile(t, "seeds.txt", "api.example.com\n") + state := filepath.Join(t.TempDir(), "state.json") + // One worker at 10 names/s cannot reach the third name before the budget + // expires, even with coarse clock resolution: its second lookup may + // finish in flight, but the third must be skipped. An explicit seed also + // makes the old gate build a second pass if no main-pass name resolves. + code, stdout, stderr := runCLIStreams(t, "-simulate", "-seed", "7", "-hit-rate", "100", + "-permute", "-w", words, "-seeds", seeds, "-format", "jsonl", "-progress=false", + "-t", "1", "-rate", "10", "-max-duration", "1ms", "-state", state, "example.com") + if code != exitOK { + t.Fatalf("budget stop: exit %d\n%s", code, stderr) + } + _, summary := jsonlRecords(t, stdout) + if len(summary.Targets) != 1 || summary.Targets[0].StoppedBy != "duration" { + t.Fatalf("expected a duration stop, got %+v", summary) + } + if summary.Targets[0].Permutation != nil || strings.Contains(stderr, "Permutation pass for") { + t.Errorf("expired main pass started permutation: %+v\n%s", summary.Targets[0].Permutation, stderr) + } + data, err := os.ReadFile(state) + if err != nil { + t.Fatal(err) + } + var saved resumeState + if err := json.Unmarshal(data, &saved); err != nil { + t.Fatal(err) + } + if len(saved.Targets) != 1 || saved.Targets[0].Done || saved.Targets[0].Permute != nil { + t.Errorf("state should leave the permutation pass unstarted: %+v", saved.Targets) + } + + // The deferred pass must still run after resuming the main pass. + code, stdout, stderr = runCLIStreams(t, "-resume", state, "-max-duration", "0", "-rate", "0") + if code != exitOK { + t.Fatalf("resume: exit %d\n%s", code, stderr) + } + _, summary = jsonlRecords(t, stdout) + if len(summary.Targets) != 1 || summary.Targets[0].Permutation == nil || summary.Targets[0].Permutation.Found == 0 || summary.Verdict != "complete" { + t.Errorf("resumed scan did not finish the deferred pass: %+v", summary) + } +} diff --git a/internal/cli/session.go b/internal/cli/session.go index 0cb24c6..e740074 100644 --- a/internal/cli/session.go +++ b/internal/cli/session.go @@ -717,10 +717,10 @@ func (s *session) scanOneTarget(ctx context.Context, i int, target string) (next } res := s.mainPass(ctx, pass, mainSaved) // -permute: a second pass over permutations of what was found (#72), - // skipped after an interrupt or a resolver abort. Its seeds are the + // skipped after an interrupt, a resolver abort or a spent time budget. Its seeds are the // main pass's names only, so a resumed pass rebuilds the same // candidate list, and it resumes from its own position (#107). - if s.f.permute && res.done && !res.resolverAbort && s.signalCode.Load() == 0 { + if s.f.permute && res.done && !res.unfinished() && s.signalCode.Load() == 0 { p := permuteInput{target: target, entries: entries, found: s.targetResults.mainPassNames(), seeds: seeds, restoredHits: restoredHits} if saved != nil { p.prev = saved.Permute diff --git a/internal/tui/passes_test.go b/internal/tui/passes_test.go index 3583774..727abaa 100644 --- a/internal/tui/passes_test.go +++ b/internal/tui/passes_test.go @@ -309,6 +309,11 @@ func TestPermutationPassStates(t *testing.T) { if m := update(start(nil), eventMsg{scan.Event{Kind: scan.EventError, Fatal: true, Message: "too many failures"}}, abortedDone); !m.scanView.done || !m.scanView.failed() { t.Error("a main pass the reliability guard aborted was followed by a permutation pass") } + expiredDone := mainDone + expiredDone.Stats.Expired, expiredDone.Stats.Skipped = true, 5 + if m := update(start(nil), expiredDone); !m.scanView.done || m.scanView.main != nil || !m.final.Stats.Expired { + t.Error("a main pass stopped by its time budget was followed by a permutation pass") + } // ctrl+c while the candidates are built: the pass does not start, and // the scan ends as an interrupted one. diff --git a/internal/tui/scan_view.go b/internal/tui/scan_view.go index 041de85..186d43f 100644 --- a/internal/tui/scan_view.go +++ b/internal/tui/scan_view.go @@ -420,9 +420,9 @@ func (m *scanViewModel) applyEvent(ev scan.Event) { m.found = ev.Found m.stats = ev.Stats // With Permute on, a finished main pass is followed by the - // permutation pass, unless the user or the reliability guard - // stopped the scan, as on the command line. - if m.permute && !m.aborted && !ev.Failed { + // permutation pass, unless the user, the reliability guard or + // the time budget stopped the scan, as on the command line. + if m.permute && !m.aborted && !ev.Failed && !ev.Stats.Expired { m.main = &ev } else { m.done = true