@@ -145,9 +145,10 @@ var ErrNoRun = errors.New("no live docker-agent run found; start one with: docke
145145// An empty target returns the most recently started run. A numeric target is
146146// matched by PID; a target starting with "http://" or "https://" is matched
147147// against record addresses; anything else is matched as a (possibly partial)
148- // session ID. Matching is exact for PID and addr, prefix-or-substring for
149- // session ID; ambiguous matches return an error so callers don't act on the
150- // wrong session.
148+ // session ID. PID and address matches are exact. Session-ID matching prefers
149+ // exact equality and only falls back to substring matching when no record
150+ // matches exactly; ambiguous substring matches return an error so callers
151+ // don't act on the wrong session.
151152func Find (target string ) (Record , error ) {
152153 target = strings .TrimSpace (target )
153154 if target == "" {
@@ -175,7 +176,7 @@ func Find(target string) (Record, error) {
175176 return r , nil
176177 }
177178 }
178- return Record {}, fmt .Errorf ("no live run with pid %d" , pid )
179+ return Record {}, fmt .Errorf ("no live run with pid %d: %w " , pid , ErrNoRun )
179180 }
180181
181182 if strings .HasPrefix (target , "http://" ) || strings .HasPrefix (target , "https://" ) {
@@ -185,18 +186,25 @@ func Find(target string) (Record, error) {
185186 return r , nil
186187 }
187188 }
188- return Record {}, fmt .Errorf ("no live run at %s" , target )
189+ return Record {}, fmt .Errorf ("no live run at %s: %w " , target , ErrNoRun )
189190 }
190191
192+ // Prefer an exact session-id match: an unambiguous full id must always
193+ // resolve, even when other ids contain it as a substring.
194+ for _ , r := range records {
195+ if r .SessionID == target {
196+ return r , nil
197+ }
198+ }
191199 var matches []Record
192200 for _ , r := range records {
193- if r . SessionID == target || strings . HasPrefix ( r . SessionID , target ) || strings .Contains (r .SessionID , target ) {
201+ if strings .Contains (r .SessionID , target ) {
194202 matches = append (matches , r )
195203 }
196204 }
197205 switch len (matches ) {
198206 case 0 :
199- return Record {}, fmt .Errorf ("no live run matches %q (pid, http URL, or session id)" , target )
207+ return Record {}, fmt .Errorf ("no live run matches %q (pid, http URL, or session id): %w " , target , ErrNoRun )
200208 case 1 :
201209 return matches [0 ], nil
202210 default :
0 commit comments