refactor(costs): rename wisp references to entries

- querySessionCostWisps → querySessionCostEntries
- Rename variables: wisps → costEntries, todayWisps → todayEntries
- Add comment explaining POSIX O_APPEND atomicity guarantee

Follow-up to code review feedback.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
joe
2026-01-24 21:29:57 -08:00
committed by Steve Yegge
parent dc3fd47a32
commit 2a0420177e
+18 -16
View File
@@ -280,7 +280,7 @@ func runCostsFromLedger() error {
if costsToday { if costsToday {
// For today: query ephemeral wisps (not yet digested) // For today: query ephemeral wisps (not yet digested)
// This gives real-time view of today's costs // This gives real-time view of today's costs
entries, err = querySessionCostWisps(now) entries, err = querySessionCostEntries(now)
if err != nil { if err != nil {
return fmt.Errorf("querying session cost wisps: %w", err) return fmt.Errorf("querying session cost wisps: %w", err)
} }
@@ -293,8 +293,8 @@ func runCostsFromLedger() error {
} }
// Also include today's wisps (not yet digested) // Also include today's wisps (not yet digested)
todayWisps, _ := querySessionCostWisps(now) todayEntries, _ := querySessionCostEntries(now)
entries = append(entries, todayWisps...) entries = append(entries, todayEntries...)
} else { } else {
// No time filter: query both digests and legacy session.ended events // No time filter: query both digests and legacy session.ended events
// (for backwards compatibility during migration) // (for backwards compatibility during migration)
@@ -821,7 +821,9 @@ func runCostsRecord(cmd *cobra.Command, args []string) error {
return fmt.Errorf("creating log directory: %w", err) return fmt.Errorf("creating log directory: %w", err)
} }
// Open file for append (create if doesn't exist) // Open file for append (create if doesn't exist).
// O_APPEND writes are atomic on POSIX for writes < PIPE_BUF (~4KB).
// A JSON log entry is ~200 bytes, so concurrent appends are safe.
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return fmt.Errorf("opening costs log: %w", err) return fmt.Errorf("opening costs log: %w", err)
@@ -934,13 +936,13 @@ func runCostsDigest(cmd *cobra.Command, args []string) error {
dateStr := targetDate.Format("2006-01-02") dateStr := targetDate.Format("2006-01-02")
// Query ephemeral session.ended wisps for target date // Query session cost entries for target date
wisps, err := querySessionCostWisps(targetDate) costEntries, err := querySessionCostEntries(targetDate)
if err != nil { if err != nil {
return fmt.Errorf("querying session cost wisps: %w", err) return fmt.Errorf("querying session cost entries: %w", err)
} }
if len(wisps) == 0 { if len(costEntries) == 0 {
fmt.Printf("%s No session cost entries found for %s\n", style.Dim.Render("○"), dateStr) fmt.Printf("%s No session cost entries found for %s\n", style.Dim.Render("○"), dateStr)
return nil return nil
} }
@@ -948,17 +950,17 @@ func runCostsDigest(cmd *cobra.Command, args []string) error {
// Build digest // Build digest
digest := CostDigest{ digest := CostDigest{
Date: dateStr, Date: dateStr,
Sessions: wisps, Sessions: costEntries,
ByRole: make(map[string]float64), ByRole: make(map[string]float64),
ByRig: make(map[string]float64), ByRig: make(map[string]float64),
} }
for _, w := range wisps { for _, e := range costEntries {
digest.TotalUSD += w.CostUSD digest.TotalUSD += e.CostUSD
digest.SessionCount++ digest.SessionCount++
digest.ByRole[w.Role] += w.CostUSD digest.ByRole[e.Role] += e.CostUSD
if w.Rig != "" { if e.Rig != "" {
digest.ByRig[w.Rig] += w.CostUSD digest.ByRig[e.Rig] += e.CostUSD
} }
} }
@@ -1000,8 +1002,8 @@ func runCostsDigest(cmd *cobra.Command, args []string) error {
return nil return nil
} }
// querySessionCostWisps reads session cost entries from the local log file for a target date. // querySessionCostEntries reads session cost entries from the local log file for a target date.
func querySessionCostWisps(targetDate time.Time) ([]CostEntry, error) { func querySessionCostEntries(targetDate time.Time) ([]CostEntry, error) {
logPath := getCostsLogPath() logPath := getCostsLogPath()
// Read log file // Read log file