Files
gastown/internal/session/stale.go
Artem Bambalov 533caf8e4b Detect and clean stale POLECAT_DONE messages (#913)
* fix(witness): detect and ignore stale POLECAT_DONE messages

Add timestamp validation to prevent witness from nuking newly spawned
polecat sessions when processing stale POLECAT_DONE messages from
previous sessions.

- Add isStalePolecatDone() to compare message timestamp vs session created time
- If message timestamp < session created time, message is stale and ignored
- Add unit tests for timestamp parsing and stale detection logic

Fixes #909

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat(mail): add --stale flag to gt mail archive

Add ability to archive stale messages (sent before current session started).
This prevents old messages from cycling forever in patrol inbox.

Changes:
- Add --stale and --dry-run flags to gt mail archive
- Move stale detection helpers to internal/session/stale.go for reuse
- Add ParseAddress to parse mail addresses into AgentIdentity
- Add SessionCreatedAt to get tmux session start time

Usage:
  gt mail archive --stale           # Archive all stale messages
  gt mail archive --stale --dry-run # Preview what would be archived

Co-Authored-By: GPT-5.2 Codex <noreply@openai.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: GPT-5.2 Codex <noreply@openai.com>
2026-01-24 21:47:59 -08:00

47 lines
1.2 KiB
Go

package session
import (
"fmt"
"strings"
"time"
"github.com/steveyegge/gastown/internal/tmux"
)
// SessionCreatedAt returns the time a tmux session was created.
func SessionCreatedAt(sessionName string) (time.Time, error) {
t := tmux.NewTmux()
info, err := t.GetSessionInfo(sessionName)
if err != nil {
return time.Time{}, err
}
return ParseTmuxSessionCreated(info.Created)
}
// ParseTmuxSessionCreated parses the tmux session created timestamp.
func ParseTmuxSessionCreated(created string) (time.Time, error) {
created = strings.TrimSpace(created)
if created == "" {
return time.Time{}, fmt.Errorf("empty session created time")
}
return time.ParseInLocation("2006-01-02 15:04:05", created, time.Local)
}
// StaleReasonForTimes compares message time to session creation and returns staleness info.
func StaleReasonForTimes(messageTime, sessionCreated time.Time) (bool, string) {
if messageTime.IsZero() || sessionCreated.IsZero() {
return false, ""
}
if messageTime.Before(sessionCreated) {
reason := fmt.Sprintf("message=%s session_started=%s",
messageTime.Format(time.RFC3339),
sessionCreated.Format(time.RFC3339),
)
return true, reason
}
return false, ""
}