(ctx context.Context, taskID string, actor ActorContext)
| 194 | } |
| 195 | |
| 196 | func (m *Service) requireAgentSessionTaskLease(ctx context.Context, taskID string, actor ActorContext) error { |
| 197 | if actor.Actor.Kind.Normalize() != ActorKindAgentSession { |
| 198 | return nil |
| 199 | } |
| 200 | sessionID := strings.TrimSpace(actor.Actor.Ref) |
| 201 | normalizedTaskID := strings.TrimSpace(taskID) |
| 202 | if sessionID == "" { |
| 203 | return autonomyError(AutonomySessionRequired, ErrPermissionDenied, "agent session identity is required") |
| 204 | } |
| 205 | if normalizedTaskID == "" { |
| 206 | return fmt.Errorf("%w: task.id is required", ErrValidation) |
| 207 | } |
| 208 | leaseStore, ok := m.store.(AutonomyLeaseStore) |
| 209 | if !ok { |
| 210 | return errors.New("task: autonomy lease lookup store is unavailable") |
| 211 | } |
| 212 | handles, err := leaseStore.ListAutonomyLeaseHandles(ctx, sessionID) |
| 213 | if err != nil { |
| 214 | return err |
| 215 | } |
| 216 | activeCount := 0 |
| 217 | matched := false |
| 218 | now := m.now().UTC() |
| 219 | for _, handle := range handles { |
| 220 | normalized := normalizeAutonomyLeaseHandle(handle) |
| 221 | if !isScopedTaskLeaseActive(normalized, sessionID, now) { |
| 222 | continue |
| 223 | } |
| 224 | activeCount++ |
| 225 | if normalized.TaskID == normalizedTaskID { |
| 226 | matched = true |
| 227 | } |
| 228 | } |
| 229 | switch { |
| 230 | case activeCount > 1: |
| 231 | return autonomyError( |
| 232 | AutonomyLeaseAlreadyHeld, |
| 233 | ErrActiveRunLease, |
| 234 | "session %q owns multiple active task-run leases", |
| 235 | sessionID, |
| 236 | ) |
| 237 | case matched: |
| 238 | return nil |
| 239 | case activeCount == 1: |
| 240 | return autonomyError( |
| 241 | AutonomyForeignRun, |
| 242 | ErrPermissionDenied, |
| 243 | "task %q is not leased by session %q", |
| 244 | normalizedTaskID, |
| 245 | sessionID, |
| 246 | ) |
| 247 | default: |
| 248 | return autonomyError( |
| 249 | AutonomyNoActiveLease, |
| 250 | ErrInvalidClaimToken, |
| 251 | "session %q has no active task-run lease", |
| 252 | sessionID, |
| 253 | ) |
no test coverage detected