|
| 1 | +package bots |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/memohai/memoh/internal/db" |
| 12 | + "github.com/memohai/memoh/internal/db/postgres/sqlc" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + botWorkspaceMetadataKey = "workspace" |
| 17 | + botLastSetupErrorMetadataKey = "last_setup_error" |
| 18 | + botSetupFailureMessageMaxRune = 4096 |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + diagnosticURLUserinfoPattern = regexp.MustCompile(`([A-Za-z][A-Za-z0-9+.-]*://)([^/\s:@]+):([^/\s@]+)@`) |
| 23 | + diagnosticSecretParamPattern = regexp.MustCompile(`(?i)\b(token|password|passwd|pwd|secret|api_key|access_token)=([^&\s]+)`) |
| 24 | +) |
| 25 | + |
| 26 | +type containerSetupFailure struct { |
| 27 | + Phase string |
| 28 | + Message string |
| 29 | + At string |
| 30 | +} |
| 31 | + |
| 32 | +// RecordContainerSetupFailure persists a sanitized container setup failure so |
| 33 | +// runtime diagnostics can explain why a ready bot is unhealthy. |
| 34 | +func (s *Service) RecordContainerSetupFailure(ctx context.Context, botID, phase string, setupErr error) error { |
| 35 | + if s.queries == nil { |
| 36 | + return errors.New("bot queries not configured") |
| 37 | + } |
| 38 | + botUUID, err := db.ParseUUID(botID) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + row, err := s.queries.GetBotByID(ctx, botUUID) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + botRow := asSQLCBot(row) |
| 47 | + metadata, err := decodeMetadata(botRow.Metadata) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + workspace := cloneMetadataSection(metadata[botWorkspaceMetadataKey]) |
| 52 | + workspace[botLastSetupErrorMetadataKey] = map[string]any{ |
| 53 | + "phase": normalizeSetupFailurePhase(phase), |
| 54 | + "message": sanitizeSetupFailureMessage(errorMessage(setupErr)), |
| 55 | + "at": time.Now().UTC().Format(time.RFC3339), |
| 56 | + } |
| 57 | + metadata[botWorkspaceMetadataKey] = workspace |
| 58 | + return s.persistBotMetadata(ctx, botRow, metadata) |
| 59 | +} |
| 60 | + |
| 61 | +// ClearContainerSetupFailure removes stale setup failure diagnostics after a |
| 62 | +// successful container setup or manual container creation. |
| 63 | +func (s *Service) ClearContainerSetupFailure(ctx context.Context, botID string) error { |
| 64 | + if s.queries == nil { |
| 65 | + return errors.New("bot queries not configured") |
| 66 | + } |
| 67 | + botUUID, err := db.ParseUUID(botID) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + row, err := s.queries.GetBotByID(ctx, botUUID) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + botRow := asSQLCBot(row) |
| 76 | + metadata, err := decodeMetadata(botRow.Metadata) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + workspace, ok := metadata[botWorkspaceMetadataKey].(map[string]any) |
| 81 | + if !ok { |
| 82 | + return nil |
| 83 | + } |
| 84 | + if _, ok := workspace[botLastSetupErrorMetadataKey]; !ok { |
| 85 | + return nil |
| 86 | + } |
| 87 | + workspace = cloneMetadataSection(workspace) |
| 88 | + delete(workspace, botLastSetupErrorMetadataKey) |
| 89 | + metadata[botWorkspaceMetadataKey] = workspace |
| 90 | + return s.persistBotMetadata(ctx, botRow, metadata) |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Service) persistBotMetadata(ctx context.Context, row sqlc.Bot, metadata map[string]any) error { |
| 94 | + payload, err := json.Marshal(metadata) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + _, err = s.queries.UpdateBotProfile(ctx, sqlc.UpdateBotProfileParams{ |
| 99 | + ID: row.ID, |
| 100 | + Name: row.Name, |
| 101 | + DisplayName: row.DisplayName, |
| 102 | + AvatarUrl: row.AvatarUrl, |
| 103 | + Timezone: row.Timezone, |
| 104 | + IsActive: row.IsActive, |
| 105 | + Metadata: payload, |
| 106 | + }) |
| 107 | + return err |
| 108 | +} |
| 109 | + |
| 110 | +func lastContainerSetupFailure(payload []byte) (containerSetupFailure, bool, error) { |
| 111 | + metadata, err := decodeMetadata(payload) |
| 112 | + if err != nil { |
| 113 | + return containerSetupFailure{}, false, err |
| 114 | + } |
| 115 | + workspace, ok := metadata[botWorkspaceMetadataKey].(map[string]any) |
| 116 | + if !ok { |
| 117 | + return containerSetupFailure{}, false, nil |
| 118 | + } |
| 119 | + raw, ok := workspace[botLastSetupErrorMetadataKey].(map[string]any) |
| 120 | + if !ok { |
| 121 | + return containerSetupFailure{}, false, nil |
| 122 | + } |
| 123 | + failure := containerSetupFailure{ |
| 124 | + Phase: stringValue(raw["phase"]), |
| 125 | + Message: stringValue(raw["message"]), |
| 126 | + At: stringValue(raw["at"]), |
| 127 | + } |
| 128 | + if strings.TrimSpace(failure.Message) == "" { |
| 129 | + return containerSetupFailure{}, false, nil |
| 130 | + } |
| 131 | + return failure, true, nil |
| 132 | +} |
| 133 | + |
| 134 | +func (f containerSetupFailure) metadata() map[string]any { |
| 135 | + data := map[string]any{ |
| 136 | + "setup_error_phase": f.Phase, |
| 137 | + "setup_error_at": f.At, |
| 138 | + } |
| 139 | + return data |
| 140 | +} |
| 141 | + |
| 142 | +func cloneMetadataSection(raw any) map[string]any { |
| 143 | + section := make(map[string]any) |
| 144 | + if existing, ok := raw.(map[string]any); ok { |
| 145 | + for key, value := range existing { |
| 146 | + section[key] = value |
| 147 | + } |
| 148 | + } |
| 149 | + return section |
| 150 | +} |
| 151 | + |
| 152 | +func normalizeSetupFailurePhase(phase string) string { |
| 153 | + switch strings.TrimSpace(phase) { |
| 154 | + case "image_prepare": |
| 155 | + return "image_prepare" |
| 156 | + case "start": |
| 157 | + return "start" |
| 158 | + default: |
| 159 | + return "setup" |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func sanitizeSetupFailureMessage(message string) string { |
| 164 | + message = strings.TrimSpace(message) |
| 165 | + if message == "" { |
| 166 | + message = "container setup failed" |
| 167 | + } |
| 168 | + message = diagnosticURLUserinfoPattern.ReplaceAllString(message, "${1}***:***@") |
| 169 | + message = diagnosticSecretParamPattern.ReplaceAllString(message, "${1}=***") |
| 170 | + runes := []rune(message) |
| 171 | + if len(runes) > botSetupFailureMessageMaxRune { |
| 172 | + message = string(runes[:botSetupFailureMessageMaxRune]) |
| 173 | + } |
| 174 | + return message |
| 175 | +} |
| 176 | + |
| 177 | +func errorMessage(err error) string { |
| 178 | + if err == nil { |
| 179 | + return "" |
| 180 | + } |
| 181 | + return err.Error() |
| 182 | +} |
| 183 | + |
| 184 | +func stringValue(value any) string { |
| 185 | + if text, ok := value.(string); ok { |
| 186 | + return strings.TrimSpace(text) |
| 187 | + } |
| 188 | + return "" |
| 189 | +} |
0 commit comments