runid.go

 1package agent
 2
 3import "context"
 4
 5// runIDContextKey is the unexported context key used to carry a
 6// caller-supplied RunID from the workspace HTTP boundary
 7// (backend.SendMessage) down into coordinator.Run without forcing a
 8// breaking change to the Coordinator.Run signature. The value is
 9// then copied onto SessionAgentCall.RunID by the coordinator so the
10// agent's terminal RunComplete event can echo it back to the
11// originating caller.
12type runIDContextKey struct{}
13
14// WithRunID returns ctx tagged with a per-request RunID. It is the
15// boundary helper for callers that need their SendMessage→Run
16// terminal event to be uniquely correlatable (e.g. `crush run`
17// against a session that may be busy). Empty runIDs are stored
18// as-is; downstream code treats an empty RunID as "caller did not
19// supply one" and falls back to SessionID-only correlation.
20func WithRunID(ctx context.Context, runID string) context.Context {
21	return context.WithValue(ctx, runIDContextKey{}, runID)
22}
23
24// RunIDFromContext returns the RunID set by [WithRunID], or "" if
25// none was set or the value is not a string. Exported because the
26// coordinator and tests in other packages need to read it; safe to
27// call on any context.
28func RunIDFromContext(ctx context.Context) string {
29	if v, ok := ctx.Value(runIDContextKey{}).(string); ok {
30		return v
31	}
32	return ""
33}