gunk-dev/the-valley

store_helper.go

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