refactor(artwork): make the trace nil-safe in one place only

add already handles a nil trace, so record's own guard was dead; Steps was the
odd one out and would panic where every other method tolerates nil.
This commit is contained in:
Deluan 2026-08-14 14:49:11 -04:00
commit ceb0be482e
3 changed files with 5 additions and 3 deletions

View file

@ -56,9 +56,7 @@ func (c *chainState) try(candidate string, res resolution, ok bool) (resolution,
}
func (c *chainState) record(candidate string, out Outcome, detail string) {
if c.trace != nil {
c.trace.add(traceStep{Candidate: candidate, Outcome: out, Detail: detail})
}
c.trace.add(traceStep{Candidate: candidate, Outcome: out, Detail: detail})
}
// exhausted is the outcome when no source in the chain yielded an image.

View file

@ -52,6 +52,9 @@ func (t *chainTrace) add(step traceStep) {
}
func (t *chainTrace) Steps() []traceStep {
if t == nil {
return nil
}
t.mu.Lock()
defer t.mu.Unlock()
return slices.Clone(t.steps)

View file

@ -56,6 +56,7 @@ var _ = Describe("chainTrace", func() {
It("does not panic when the trace is nil", func() {
var t *chainTrace
Expect(func() { t.add(traceStep{Candidate: "cover.*", Outcome: OutcomeMiss}) }).ToNot(Panic())
Expect(t.Steps()).To(BeEmpty(), "a nil trace collects nothing, so reading it must be as safe as writing it")
})
It("is safe to use concurrently", func() {