88	"io" 
99	"net" 
1010	"os" 
11- 	"slices" 
12- 	"strconv" 
13- 	"strings" 
1411	"time" 
1512
1613	"github.com/containerd/console" 
@@ -37,51 +34,20 @@ type traceOptions struct {
3734}
3835
3936func  loadTrace (ctx  context.Context , ref  string , nodes  []builder.Node ) (string , []byte , error ) {
40- 	var  offset  * int 
41- 	if  strings .HasPrefix (ref , "^" ) {
42- 		off , err  :=  strconv .Atoi (ref [1 :])
43- 		if  err  !=  nil  {
44- 			return  "" , nil , errors .Wrapf (err , "invalid offset %q" , ref )
45- 		}
46- 		offset  =  & off 
47- 		ref  =  "" 
48- 	}
49- 
50- 	recs , err  :=  queryRecords (ctx , ref , nodes )
37+ 	recs , err  :=  queryRecords (ctx , ref , nodes , & queryOptions {
38+ 		CompletedOnly : true ,
39+ 	})
5140	if  err  !=  nil  {
5241		return  "" , nil , err 
5342	}
5443
55- 	var  rec  * historyRecord 
56- 
57- 	if  ref  ==  ""  {
58- 		slices .SortFunc (recs , func (a , b  historyRecord ) int  {
59- 			return  b .CreatedAt .AsTime ().Compare (a .CreatedAt .AsTime ())
60- 		})
61- 		for  _ , r  :=  range  recs  {
62- 			if  r .CompletedAt  !=  nil  {
63- 				if  offset  !=  nil  {
64- 					if  * offset  >  0  {
65- 						* offset -- 
66- 						continue 
67- 					}
68- 				}
69- 				rec  =  & r 
70- 				break 
71- 			}
72- 		}
73- 		if  offset  !=  nil  &&  * offset  >  0  {
74- 			return  "" , nil , errors .Errorf ("no completed build found with offset %d" , * offset )
75- 		}
76- 	} else  {
77- 		rec  =  & recs [0 ]
78- 	}
79- 	if  rec  ==  nil  {
44+ 	if  len (recs ) ==  0  {
8045		if  ref  ==  ""  {
8146			return  "" , nil , errors .New ("no records found" )
8247		}
8348		return  "" , nil , errors .Errorf ("no record found for ref %q" , ref )
8449	}
50+ 	rec  :=  & recs [0 ]
8551
8652	if  rec .CompletedAt  ==  nil  {
8753		return  "" , nil , errors .Errorf ("build %q is not completed, only completed builds can be traced" , rec .Ref )
@@ -103,7 +69,9 @@ func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, [
10369			return  "" , nil , err 
10470		}
10571
106- 		recs , err  :=  queryRecords (ctx , rec .Ref , []builder.Node {* rec .node })
72+ 		recs , err  :=  queryRecords (ctx , rec .Ref , []builder.Node {* rec .node }, & queryOptions {
73+ 			CompletedOnly : true ,
74+ 		})
10775		if  err  !=  nil  {
10876			return  "" , nil , err 
10977		}
0 commit comments