| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "the-valley/flow/internal/flowenv" |
| 7 | "the-valley/flow/internal/run" |
| 8 | ) |
| 9 | |
| 10 | // sessionIDFromEnv returns the current session ID from VALLEY_FLOW_SESSION_ID |
| 11 | // or KLAUS_SESSION_ID. |
| 12 | func sessionIDFromEnv() string { return flowenv.SessionID.Get() } |
| 13 | |
| 14 | // tmuxSessionEnvPrefix returns a shell snippet that exports the session ID |
| 15 | // for use in tmux pane commands. Tmux panes start fresh shells that don't |
| 16 | // inherit the caller's environment, so any env vars they need must be |
| 17 | // explicitly exported in the command string. Every process an agent starts in |
| 18 | // its worktree inherits the ID, `go test` included, so anything that turns it |
| 19 | // into a tmux target (agentsSessionName) must never be handed a real tmux |
| 20 | // client from a test; see docs/development.md. |
| 21 | func tmuxSessionEnvPrefix() string { |
| 22 | sessionID := sessionIDFromEnv() |
| 23 | if sessionID == "" { |
| 24 | return "" |
| 25 | } |
| 26 | return flowenv.SessionID.Export(sessionID) |
| 27 | } |
| 28 | |
| 29 | // sessionStore returns a HomeDirStore for the current session (from |
| 30 | // VALLEY_FLOW_SESSION_ID or KLAUS_SESSION_ID). This is the canonical way to get |
| 31 | // the store for the active session. |
| 32 | func sessionStore() (run.StateStore, error) { |
| 33 | sessionID := sessionIDFromEnv() |
| 34 | if sessionID == "" { |
| 35 | // Fall back to most recent session |
| 36 | sessionsDir, err := run.SessionsDir() |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("KLAUS_SESSION_ID is not set and could not find sessions directory: %w", err) |
| 39 | } |
| 40 | found, err := run.FindMostRecentSession(sessionsDir) |
| 41 | if err != nil { |
| 42 | return nil, fmt.Errorf("KLAUS_SESSION_ID is not set and no sessions found (are you inside a flow session?)") |
| 43 | } |
| 44 | sessionID = found |
| 45 | } |
| 46 | store, err := run.NewHomeDirStore(sessionID) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | return store, nil |
| 51 | } |