fix(ingest): preserve active_hours when backfill sends zero

On conflict update now skips active_hours if incoming value is 0,
preventing the cost-backfill script from clobbering existing session
hours with its sentinel 0.0 value.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vadym Samoilenko 2026-05-13 13:13:48 +01:00
parent 0148b81e9c
commit ff8b20857f

View file

@ -52,6 +52,23 @@ async def ingest(
project.repo_url = s.repo_url
# Upsert session (dedup by user_id + session_id + date)
update_fields: dict = dict(
end_at=s.end_at,
message_count=s.message_count,
work_summary=s.work_summary,
commits=s.commits,
tools_used=s.tools_used,
files_changed=s.files_changed,
raw_stats=s.raw_stats,
input_tokens=s.input_tokens,
output_tokens=s.output_tokens,
cost_usd=s.cost_usd,
)
# Only overwrite active_hours when the collector sends a real value (>0).
# The cost-backfill script sends 0 to avoid clobbering existing hours.
if s.active_hours > 0:
update_fields["active_hours"] = s.active_hours
stmt = insert(Session).values(
user_id=user.id,
project_id=project.id,
@ -71,19 +88,7 @@ async def ingest(
cost_usd=s.cost_usd,
).on_conflict_do_update(
constraint="uq_session_user_date",
set_=dict(
end_at=s.end_at,
active_hours=s.active_hours,
message_count=s.message_count,
work_summary=s.work_summary,
commits=s.commits,
tools_used=s.tools_used,
files_changed=s.files_changed,
raw_stats=s.raw_stats,
input_tokens=s.input_tokens,
output_tokens=s.output_tokens,
cost_usd=s.cost_usd,
),
set_=update_fields,
)
result = await db.execute(stmt)
if result.rowcount: