rdev/internal/domain/session.go
jordan 7249575dea
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat(sessions): add command execution endpoint and activity tracking
- Add POST /sessions/:id/exec endpoint for executing commands in sessions
- Add session activity tracking (last_activity_at timestamp)
- Add database migration 024 for session activity column
- Add comprehensive tests for session handlers and service layer
- Add wildcard TLS certificate for preview.threesix.ai subdomain
- Add infrastructure mocks for testing preview service
- Refactor preview cleanup logic to remove unused methods
- Add AIOS core documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-13 08:41:05 -07:00

87 lines
2.6 KiB
Go

// Package domain contains pure domain models with no external dependencies.
package domain
import "time"
// SessionID is a strongly-typed identifier for sessions.
type SessionID string
// SessionStatus represents the state of a session.
type SessionStatus string
const (
// SessionStatusActive indicates the session is currently in use.
SessionStatusActive SessionStatus = "active"
// SessionStatusEnded indicates the session was completed via checkin.
SessionStatusEnded SessionStatus = "ended"
// SessionStatusExpired indicates the session expired without checkin.
SessionStatusExpired SessionStatus = "expired"
)
// Session represents an interactive remote development session.
// A session composes a checkout (git token + branch), pod binding, and ephemeral preview URL.
type Session struct {
// ID is the unique session identifier.
ID SessionID
// ProjectID is the project this session belongs to.
ProjectID ProjectID
// CheckoutID links to the checkout that provides git access.
CheckoutID CheckoutID
// PodName is the bound project pod for command execution.
PodName string
// PreviewURL is the full HTTPS URL for the ephemeral preview.
// Format: https://{session-slug}.preview.threesix.ai
PreviewURL string
// PreviewHost is the hostname for the K8s Ingress.
// Format: {session-slug}.preview.threesix.ai
PreviewHost string
// CreatedBy is the user or API key that created the session.
CreatedBy string
// CreatedAt is when the session was created.
CreatedAt time.Time
// ExpiresAt is when the session will automatically expire.
ExpiresAt time.Time
// Status is the current state of the session.
Status SessionStatus
// LastActivityAt is the most recent command or interaction timestamp.
LastActivityAt time.Time
// EndedAt is when the session was ended (if ended or expired).
EndedAt *time.Time
}
// IsActive returns true if the session can still be used.
func (s *Session) IsActive() bool {
return s.Status == SessionStatusActive && time.Now().Before(s.ExpiresAt)
}
// IsExpired returns true if the session has expired.
func (s *Session) IsExpired() bool {
return s.Status == SessionStatusActive && time.Now().After(s.ExpiresAt)
}
// IsExpiredWithGrace returns true if the session has expired and the grace period
// since the last activity has also elapsed. This prevents expiring sessions that
// are still actively being used.
func (s *Session) IsExpiredWithGrace(gracePeriod time.Duration) bool {
if s.Status != SessionStatusActive {
return false
}
if time.Now().Before(s.ExpiresAt) {
return false
}
return time.Since(s.LastActivityAt) > gracePeriod
}