gunk-dev/the-valley

store_helper.go

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