feat(costs): Wire up gt costs record to Stop hook
Add Claude Code Stop hook that automatically records session costs when sessions end. The hook calls `gt costs record` which now can derive the session name from GT_* environment variables (GT_RIG, GT_POLECAT, GT_CREW, GT_ROLE). Changes: - Add deriveSessionName() to infer tmux session name from environment - Add Stop hook to settings-autonomous.json and settings-interactive.json - Add unit tests for deriveSessionName function When a Gas Town session ends, the Stop hook fires and records the session cost as a bead event. Costs then appear in `gt costs --today` output. Closes: gt-ntzhc 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+40
-2
@@ -519,11 +519,14 @@ func runCostsRecord(cmd *cobra.Command, args []string) error {
|
||||
// Get session from flag or try to detect from environment
|
||||
session := recordSession
|
||||
if session == "" {
|
||||
// Try to get from TMUX_PANE or tmux environment
|
||||
session = os.Getenv("GT_SESSION")
|
||||
}
|
||||
if session == "" {
|
||||
return fmt.Errorf("--session flag required (or set GT_SESSION env var)")
|
||||
// Derive session name from GT_* environment variables
|
||||
session = deriveSessionName()
|
||||
}
|
||||
if session == "" {
|
||||
return fmt.Errorf("--session flag required (or set GT_SESSION env var, or GT_RIG/GT_ROLE)")
|
||||
}
|
||||
|
||||
t := tmux.NewTmux()
|
||||
@@ -610,6 +613,41 @@ func runCostsRecord(cmd *cobra.Command, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// deriveSessionName derives the tmux session name from GT_* environment variables.
|
||||
// Session naming patterns:
|
||||
// - Polecats: gt-{rig}-{polecat} (e.g., gt-gastown-toast)
|
||||
// - Crew: gt-{rig}-crew-{crew} (e.g., gt-gastown-crew-max)
|
||||
// - Witness/Refinery: gt-{rig}-{role} (e.g., gt-gastown-witness)
|
||||
// - Mayor/Deacon: gt-{role} (e.g., gt-mayor)
|
||||
func deriveSessionName() string {
|
||||
role := os.Getenv("GT_ROLE")
|
||||
rig := os.Getenv("GT_RIG")
|
||||
polecat := os.Getenv("GT_POLECAT")
|
||||
crew := os.Getenv("GT_CREW")
|
||||
|
||||
// Polecat: gt-{rig}-{polecat}
|
||||
if polecat != "" && rig != "" {
|
||||
return fmt.Sprintf("gt-%s-%s", rig, polecat)
|
||||
}
|
||||
|
||||
// Crew: gt-{rig}-crew-{crew}
|
||||
if crew != "" && rig != "" {
|
||||
return fmt.Sprintf("gt-%s-crew-%s", rig, crew)
|
||||
}
|
||||
|
||||
// Global roles without rig: gt-{role}
|
||||
if role != "" && rig == "" {
|
||||
return fmt.Sprintf("gt-%s", role)
|
||||
}
|
||||
|
||||
// Rig-based roles (witness, refinery): gt-{rig}-{role}
|
||||
if role != "" && rig != "" {
|
||||
return fmt.Sprintf("gt-%s-%s", rig, role)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildAgentPath builds the agent path from role, rig, and worker.
|
||||
// Examples: "mayor", "gastown/witness", "gastown/polecats/toast"
|
||||
func buildAgentPath(role, rig, worker string) string {
|
||||
|
||||
Reference in New Issue
Block a user