AttachRunSession binds one existing session to a claimed or starting run.
( ctx context.Context, runID string, sessionID string, actor ActorContext, )
| 2014 | |
| 2015 | // AttachRunSession binds one existing session to a claimed or starting run. |
| 2016 | func (m *Service) AttachRunSession( |
| 2017 | ctx context.Context, |
| 2018 | runID string, |
| 2019 | sessionID string, |
| 2020 | actor ActorContext, |
| 2021 | ) (*Run, error) { |
| 2022 | if err := requireWriteAuthority(actor); err != nil { |
| 2023 | return nil, err |
| 2024 | } |
| 2025 | if err := m.requireSessionExecutor("attach run session"); err != nil { |
| 2026 | return nil, err |
| 2027 | } |
| 2028 | |
| 2029 | trimmedSessionID := strings.TrimSpace(sessionID) |
| 2030 | if trimmedSessionID == "" { |
| 2031 | return nil, fmt.Errorf("%w: session id is required", ErrValidation) |
| 2032 | } |
| 2033 | |
| 2034 | run, taskRecord, err := m.loadRunWithTask(ctx, runID) |
| 2035 | if err != nil { |
| 2036 | return nil, err |
| 2037 | } |
| 2038 | if err := m.ensureTaskExecutable(ctx, taskRecord); err != nil { |
| 2039 | return nil, err |
| 2040 | } |
| 2041 | if err := m.validateRunChannelUsable(ctx, taskRecord, run, actor, "attach"); err != nil { |
| 2042 | return nil, err |
| 2043 | } |
| 2044 | if strings.TrimSpace(run.SessionID) != "" { |
| 2045 | return nil, ErrSessionAlreadyBound |
| 2046 | } |
| 2047 | |
| 2048 | switch run.Status.Normalize() { |
| 2049 | case TaskRunStatusClaimed, TaskRunStatusStarting: |
| 2050 | default: |
| 2051 | return nil, ErrSessionAttachNotAllowed |
| 2052 | } |
| 2053 | |
| 2054 | activeBindings, err := m.store.CountActiveSessionBindings(ctx, trimmedSessionID) |
| 2055 | if err != nil { |
| 2056 | return nil, err |
| 2057 | } |
| 2058 | if activeBindings > 0 { |
| 2059 | return nil, ErrSessionAlreadyBound |
| 2060 | } |
| 2061 | |
| 2062 | sessionRef, err := m.sessions.AttachTaskSession(ctx, run.ID, trimmedSessionID) |
| 2063 | if err != nil { |
| 2064 | return nil, err |
| 2065 | } |
| 2066 | if sessionRef == nil { |
| 2067 | return nil, fmt.Errorf("%w: attach_task_session returned nil session reference", ErrValidation) |
| 2068 | } |
| 2069 | if err := sessionRef.Validate(); err != nil { |
| 2070 | return nil, err |
| 2071 | } |
| 2072 | |
| 2073 | run.SessionID = strings.TrimSpace(sessionRef.SessionID) |
nothing calls this directly
no test coverage detected