rdev/internal/domain/session.go
jordan 3dbde72966
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: add claude_id tracking and session improvements for interactive dev
- Add claude_id field to sessions (migration 026) for tracking Claude
  process IDs across pod restarts
- Extend session repository with UpdateClaudeID and session lookup methods
- Improve kubernetes executor with better error handling and exec streaming
- Add claudebox client/server improvements for session lifecycle
- Expand sessions handler with exec streaming endpoint
- Add comprehensive tests for sessions and kubernetes executor

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 00:20:32 -07:00

95 lines
2.9 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
// ClaudeSessionID is the Claude Code session ID used for --resume in subsequent turns.
// Set after the first successful claude exec in this session.
ClaudeSessionID string
// ConversationRecordID links this session to its conversation message history.
// Set on first Claude exec; messages are written to the conversations/messages tables.
ConversationRecordID string
}
// 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
}