Skip to content

Commit 58a01a2

Browse files
committed
fix(codebuff): address review findings - timestamps, freshness
1. Usage timestamp fallback: When no message timestamp is parsable, fall back to session directory timestamp, then source mtime, instead of producing year-0001 timestamps. 2. Source freshness: Added Codebuff-specific SourceMtime that computes composite max mtime across chat-messages.json, run-state.json, and chat-meta.json so companion-file-only changes are caught.
1 parent c167746 commit 58a01a2

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

internal/parser/codebuff.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,26 @@ func parseCodebuffSession(
197197
// empty. Cost is tracked at the session level via credits.
198198
if rs.CreditsUsed > 0 {
199199
cost := rs.CreditsUsed * 0.01
200+
// Determine occurred_at: prefer message timestamps, then fall
201+
// back to the session directory timestamp, then source mtime.
202+
occurredAt := startedAt
203+
if !endedAt.IsZero() {
204+
occurredAt = endedAt
205+
}
206+
if occurredAt.IsZero() && !sessionDate.IsZero() {
207+
occurredAt = sessionDate
208+
}
209+
if occurredAt.IsZero() && fileInfo.Mtime > 0 {
210+
occurredAt = time.Unix(0, fileInfo.Mtime)
211+
}
200212
sess.UsageEvents = []ParsedUsageEvent{{
201-
SessionID: fullID,
202-
Source: "session",
203-
OccurredAt: func() string {
204-
if !endedAt.IsZero() {
205-
return endedAt.Format(time.RFC3339Nano)
206-
}
207-
return startedAt.Format(time.RFC3339Nano)
208-
}(),
209-
CostUSD: &cost,
210-
CostStatus: "reported",
211-
CostSource: "session",
212-
DedupKey: "session:" + fullID,
213+
SessionID: fullID,
214+
Source: "session",
215+
OccurredAt: occurredAt.Format(time.RFC3339Nano),
216+
CostUSD: &cost,
217+
CostStatus: "reported",
218+
CostSource: "session",
219+
DedupKey: "session:" + fullID,
213220
}}
214221
}
215222

internal/sync/engine.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12156,6 +12156,26 @@ func (e *Engine) SourceMtime(sessionID string) int64 {
1215612156
_, mtime := roocodeEffectiveStat(path, info)
1215712157
return mtime
1215812158
}
12159+
if def.Type == parser.AgentCodebuff {
12160+
// Freshness spans chat-messages.json plus run-state.json and
12161+
// chat-meta.json. Compute the composite max mtime across all
12162+
// three files so companion-file-only changes are caught.
12163+
info, err := os.Stat(path)
12164+
if err != nil {
12165+
return 0
12166+
}
12167+
mtime := info.ModTime().UnixNano()
12168+
dir := filepath.Dir(path)
12169+
for _, name := range []string{"run-state.json", "chat-meta.json"} {
12170+
companion := filepath.Join(dir, name)
12171+
if ci, err := os.Stat(companion); err == nil {
12172+
if cm := ci.ModTime().UnixNano(); cm > mtime {
12173+
mtime = cm
12174+
}
12175+
}
12176+
}
12177+
return mtime
12178+
}
1215912179
if def.Type == parser.AgentKiloLegacy {
1216012180
// Freshness spans task_metadata.json (the stored path) plus
1216112181
// its siblings ui_messages.json and api_conversation_history.json.

0 commit comments

Comments
 (0)