Redesign gt seance: ZFC-compliant literal seance with predecessors (gt-7qvd7)
Major redesign based on design review: 1. REMOVED: Claude Code internal parsing (ZFC violation) - Deleted internal/claude/sessions.go (parsed ~/.claude/projects/) - This coupled us to Claude Code's undocumented internal format 2. ADDED: Event-based session discovery - gt prime now emits session_start events to ~/gt/.events.jsonl - Events include role, session_id, topic, cwd - Discovery reads our own event stream (ZFC-compliant) 3. ADDED: --talk flag for actual seances - gt seance --talk <session-id> spawns: claude --fork-session --resume <id> - --fork-session creates a new session (read-only, no grave disturbance) - You literally talk to your predecessor: "Where did you put X?" 4. ADDED: One-shot prompt mode - gt seance --talk <id> -p "Where is the config?" - Uses claude --print for quick questions The name "seance" is now literal - you commune with the dead (past sessions). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Steve Yegge
parent
35586b57e5
commit
189db8a80e
@@ -1,271 +0,0 @@
|
||||
// Package claude provides integration with Claude Code's local data.
|
||||
package claude
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SessionInfo represents a Claude Code session.
|
||||
type SessionInfo struct {
|
||||
ID string `json:"id"` // Session UUID
|
||||
Path string `json:"path"` // Decoded project path
|
||||
Role string `json:"role"` // Gas Town role (from beacon)
|
||||
Topic string `json:"topic"` // Topic (from beacon)
|
||||
StartTime time.Time `json:"start_time"` // First message timestamp
|
||||
Summary string `json:"summary"` // Session summary
|
||||
IsGasTown bool `json:"is_gastown"` // Has [GAS TOWN] beacon
|
||||
FilePath string `json:"file_path"` // Full path to JSONL file
|
||||
}
|
||||
|
||||
// SessionFilter controls which sessions are returned.
|
||||
type SessionFilter struct {
|
||||
GasTownOnly bool // Only return Gas Town sessions
|
||||
Role string // Filter by role (substring match)
|
||||
Rig string // Filter by rig name
|
||||
Path string // Filter by path (substring match)
|
||||
Limit int // Max sessions to return (0 = unlimited)
|
||||
}
|
||||
|
||||
// gasTownPattern matches the beacon: [GAS TOWN] role • topic • timestamp
|
||||
var gasTownPattern = regexp.MustCompile(`\[GAS TOWN\]\s+([^\s•]+)\s*(?:•\s*([^•]+?)\s*)?(?:•\s*(\S+))?\s*$`)
|
||||
|
||||
// DiscoverSessions finds Claude Code sessions matching the filter.
|
||||
func DiscoverSessions(filter SessionFilter) ([]SessionInfo, error) {
|
||||
claudeDir := os.ExpandEnv("$HOME/.claude")
|
||||
projectsDir := filepath.Join(claudeDir, "projects")
|
||||
|
||||
if _, err := os.Stat(projectsDir); os.IsNotExist(err) {
|
||||
return nil, nil // No sessions yet
|
||||
}
|
||||
|
||||
var sessions []SessionInfo
|
||||
|
||||
// Walk project directories
|
||||
entries, err := os.ReadDir(projectsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// Decode path from directory name
|
||||
projectPath := decodePath(entry.Name())
|
||||
|
||||
// Apply path/rig filter early
|
||||
if filter.Rig != "" && !strings.Contains(projectPath, "/"+filter.Rig+"/") {
|
||||
continue
|
||||
}
|
||||
if filter.Path != "" && !strings.Contains(projectPath, filter.Path) {
|
||||
continue
|
||||
}
|
||||
|
||||
projectDir := filepath.Join(projectsDir, entry.Name())
|
||||
sessionFiles, err := os.ReadDir(projectDir)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, sf := range sessionFiles {
|
||||
if !strings.HasSuffix(sf.Name(), ".jsonl") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip agent files (they're subprocesses, not main sessions)
|
||||
if strings.HasPrefix(sf.Name(), "agent-") {
|
||||
continue
|
||||
}
|
||||
|
||||
sessionPath := filepath.Join(projectDir, sf.Name())
|
||||
info, err := parseSession(sessionPath, projectPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Apply filters
|
||||
if filter.GasTownOnly && !info.IsGasTown {
|
||||
continue
|
||||
}
|
||||
if filter.Role != "" {
|
||||
// Check Role field first, then path
|
||||
roleMatch := strings.Contains(strings.ToLower(info.Role), strings.ToLower(filter.Role))
|
||||
pathMatch := strings.Contains(strings.ToLower(info.Path), strings.ToLower(filter.Role))
|
||||
if !roleMatch && !pathMatch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
sessions = append(sessions, info)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by start time descending (most recent first)
|
||||
sort.Slice(sessions, func(i, j int) bool {
|
||||
return sessions[i].StartTime.After(sessions[j].StartTime)
|
||||
})
|
||||
|
||||
// Apply limit
|
||||
if filter.Limit > 0 && len(sessions) > filter.Limit {
|
||||
sessions = sessions[:filter.Limit]
|
||||
}
|
||||
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
// parseSession reads a session JSONL file and extracts metadata.
|
||||
func parseSession(filePath, projectPath string) (SessionInfo, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return SessionInfo{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
info := SessionInfo{
|
||||
Path: projectPath,
|
||||
FilePath: filePath,
|
||||
}
|
||||
|
||||
// Extract session ID from filename
|
||||
base := filepath.Base(filePath)
|
||||
info.ID = strings.TrimSuffix(base, ".jsonl")
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
// Increase buffer size for large lines
|
||||
buf := make([]byte, 0, 64*1024)
|
||||
scanner.Buffer(buf, 1024*1024)
|
||||
|
||||
lineNum := 0
|
||||
for scanner.Scan() {
|
||||
lineNum++
|
||||
line := scanner.Bytes()
|
||||
|
||||
// First line is usually the summary
|
||||
if lineNum == 1 {
|
||||
var entry struct {
|
||||
Type string `json:"type"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
if err := json.Unmarshal(line, &entry); err == nil && entry.Type == "summary" {
|
||||
info.Summary = entry.Summary
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Look for user messages
|
||||
var entry struct {
|
||||
Type string `json:"type"`
|
||||
SessionID string `json:"sessionId"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Message json.RawMessage `json:"message"`
|
||||
}
|
||||
if err := json.Unmarshal(line, &entry); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if entry.Type == "user" {
|
||||
// Parse timestamp
|
||||
if entry.Timestamp != "" && info.StartTime.IsZero() {
|
||||
if t, err := time.Parse(time.RFC3339, entry.Timestamp); err == nil {
|
||||
info.StartTime = t
|
||||
}
|
||||
}
|
||||
|
||||
// Set session ID if not already set
|
||||
if info.ID == "" && entry.SessionID != "" {
|
||||
info.ID = entry.SessionID
|
||||
}
|
||||
|
||||
// Look for Gas Town beacon in message
|
||||
if !info.IsGasTown {
|
||||
msgStr := extractMessageContent(entry.Message)
|
||||
if match := gasTownPattern.FindStringSubmatch(msgStr); match != nil {
|
||||
info.IsGasTown = true
|
||||
info.Role = match[1]
|
||||
if len(match) > 2 {
|
||||
info.Topic = strings.TrimSpace(match[2])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop after finding what we need
|
||||
if info.IsGasTown && !info.StartTime.IsZero() && lineNum > 20 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// extractMessageContent extracts text content from a message JSON.
|
||||
func extractMessageContent(msg json.RawMessage) string {
|
||||
if len(msg) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Try as string first
|
||||
var str string
|
||||
if err := json.Unmarshal(msg, &str); err == nil {
|
||||
return str
|
||||
}
|
||||
|
||||
// Try as object with role/content
|
||||
var obj struct {
|
||||
Content string `json:"content"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if err := json.Unmarshal(msg, &obj); err == nil {
|
||||
return obj.Content
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// decodePath converts Claude's path-encoded directory names back to paths.
|
||||
// e.g., "-Users-stevey-gt-gastown" -> "/Users/stevey/gt/gastown"
|
||||
func decodePath(encoded string) string {
|
||||
// Replace leading dash with /
|
||||
if strings.HasPrefix(encoded, "-") {
|
||||
encoded = "/" + encoded[1:]
|
||||
}
|
||||
// Replace remaining dashes with /
|
||||
return strings.ReplaceAll(encoded, "-", "/")
|
||||
}
|
||||
|
||||
// ShortID returns a shortened version of the session ID for display.
|
||||
func (s SessionInfo) ShortID() string {
|
||||
if len(s.ID) > 8 {
|
||||
return s.ID[:8]
|
||||
}
|
||||
return s.ID
|
||||
}
|
||||
|
||||
// FormatTime returns the start time in a compact format.
|
||||
func (s SessionInfo) FormatTime() string {
|
||||
if s.StartTime.IsZero() {
|
||||
return "unknown"
|
||||
}
|
||||
return s.StartTime.Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
// RigFromPath extracts the rig name from the project path.
|
||||
func (s SessionInfo) RigFromPath() string {
|
||||
// Look for known rig patterns
|
||||
parts := strings.Split(s.Path, "/")
|
||||
for i, part := range parts {
|
||||
if part == "gt" && i+1 < len(parts) {
|
||||
// Next part after gt/ is usually the rig
|
||||
return parts[i+1]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package claude
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"-Users-stevey-gt-gastown", "/Users/stevey/gt/gastown"},
|
||||
{"-Users-stevey-gt-beads-crew-joe", "/Users/stevey/gt/beads/crew/joe"},
|
||||
{"-Users-stevey", "/Users/stevey"},
|
||||
{"foo-bar", "foo/bar"}, // Edge case: no leading dash
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
result := decodePath(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("decodePath(%q) = %q, want %q", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGasTownPattern(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldMatch bool
|
||||
role string
|
||||
topic string
|
||||
}{
|
||||
{
|
||||
input: "[GAS TOWN] gastown/polecats/furiosa • ready • 2025-12-30T22:49",
|
||||
shouldMatch: true,
|
||||
role: "gastown/polecats/furiosa",
|
||||
topic: "ready",
|
||||
},
|
||||
{
|
||||
input: "[GAS TOWN] deacon • patrol • 2025-12-30T08:00",
|
||||
shouldMatch: true,
|
||||
role: "deacon",
|
||||
topic: "patrol",
|
||||
},
|
||||
{
|
||||
input: "[GAS TOWN] gastown/crew/gus • assigned:gt-abc12 • 2025-12-30T15:42",
|
||||
shouldMatch: true,
|
||||
role: "gastown/crew/gus",
|
||||
topic: "assigned:gt-abc12",
|
||||
},
|
||||
{
|
||||
input: "Regular message without beacon",
|
||||
shouldMatch: false,
|
||||
},
|
||||
{
|
||||
input: "[GAS TOWN] witness • handoff",
|
||||
shouldMatch: true,
|
||||
role: "witness",
|
||||
topic: "handoff",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
match := gasTownPattern.FindStringSubmatch(tt.input)
|
||||
if tt.shouldMatch && match == nil {
|
||||
t.Errorf("Expected match for %q but got none", tt.input)
|
||||
return
|
||||
}
|
||||
if !tt.shouldMatch && match != nil {
|
||||
t.Errorf("Expected no match for %q but got %v", tt.input, match)
|
||||
return
|
||||
}
|
||||
if tt.shouldMatch {
|
||||
if match[1] != tt.role {
|
||||
t.Errorf("Role: got %q, want %q", match[1], tt.role)
|
||||
}
|
||||
if len(match) > 2 && match[2] != tt.topic {
|
||||
// Topic might have trailing space, trim for comparison
|
||||
gotTopic := match[2]
|
||||
if gotTopic != tt.topic {
|
||||
t.Errorf("Topic: got %q, want %q", gotTopic, tt.topic)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionInfoShortID(t *testing.T) {
|
||||
s := SessionInfo{ID: "d6d8475f-94a9-4a66-bfa6-d60126964427"}
|
||||
short := s.ShortID()
|
||||
if short != "d6d8475f" {
|
||||
t.Errorf("ShortID() = %q, want %q", short, "d6d8475f")
|
||||
}
|
||||
|
||||
s2 := SessionInfo{ID: "abc"}
|
||||
if s2.ShortID() != "abc" {
|
||||
t.Errorf("ShortID() for short ID = %q, want %q", s2.ShortID(), "abc")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInferRoleFromPath(t *testing.T) {
|
||||
s := SessionInfo{Path: "/Users/stevey/gt/gastown/crew/joe"}
|
||||
rig := s.RigFromPath()
|
||||
if rig != "gastown" {
|
||||
t.Errorf("RigFromPath() = %q, want %q", rig, "gastown")
|
||||
}
|
||||
|
||||
s2 := SessionInfo{Path: "/Users/stevey/gt/beads/polecats/jade"}
|
||||
if s2.RigFromPath() != "beads" {
|
||||
t.Errorf("RigFromPath() = %q, want %q", s2.RigFromPath(), "beads")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user