55
66import json
77import os
8+ import sys
89from base64 import urlsafe_b64decode
910from datetime import datetime , timedelta , timezone
1011from typing import Any
@@ -25,7 +26,10 @@ def load_auth() -> tuple[str, str | None]:
2526
2627def decode_jwt_account_id (token : str ) -> str | None :
2728 try :
28- payload_b64 = token .split ("." )[1 ]
29+ parts = token .split ("." )
30+ if len (parts ) != 3 :
31+ return None
32+ payload_b64 = parts [1 ]
2933 padding = - len (payload_b64 ) % 4
3034 if padding :
3135 payload_b64 += "=" * padding
@@ -62,7 +66,7 @@ def format_duration(seconds: Any) -> str:
6266 if seconds is None :
6367 return "unknown"
6468
65- total_seconds = max (0 , int (seconds ))
69+ total_seconds = max (0 , int (round ( seconds ) ))
6670 days , remainder = divmod (total_seconds , 86400 )
6771 hours , remainder = divmod (remainder , 3600 )
6872 minutes , _ = divmod (remainder , 60 )
@@ -182,13 +186,9 @@ def build_credit_items(credits: list[dict[str, Any]], now: datetime) -> list[dic
182186
183187def build_updated_item (now : datetime ) -> dict [str , Any ]:
184188 local_now = now .astimezone ()
185- if local_now .date () == datetime .now ().astimezone ().date ():
186- subtitle = local_now .strftime ("Today at %H:%M" )
187- else :
188- subtitle = local_now .strftime ("%b %-d, %H:%M" )
189189 return {
190190 "title" : "Last updated" ,
191- "subtitle" : subtitle ,
191+ "subtitle" : local_now . strftime ( "%H:%M:%S" ) ,
192192 "valid" : False ,
193193 }
194194
@@ -211,17 +211,17 @@ def fetch_usage_and_credits(headers: dict[str, str]) -> tuple[dict[str, Any] | N
211211 except HTTPError as error :
212212 if error .code == 401 :
213213 output_message ("Session expired" , "Sign in to Codex Desktop again and rerun the workflow." )
214- raise SystemExit
214+ raise SystemExit ( 0 )
215215 if error .code == 429 :
216216 output_message ("Rate limited" , "Try the workflow again in a moment." )
217- raise SystemExit
218- except Exception :
219- pass
217+ raise SystemExit ( 0 )
218+ except Exception as exc :
219+ print ( f"[codex_reset] Warning: failed to fetch usage: { exc } " , file = sys . stderr )
220220
221221 try :
222222 reset_data = fetch_json (RESET_CREDITS_URL , headers )
223- except Exception :
224- pass
223+ except Exception as exc :
224+ print ( f"[codex_reset] Warning: failed to fetch reset credits: { exc } " , file = sys . stderr )
225225
226226 return usage , reset_data
227227
@@ -240,16 +240,17 @@ def main() -> None:
240240 headers = get_headers (token , effective_account_id )
241241 usage , reset_data = fetch_usage_and_credits (headers )
242242
243+ now = datetime .now (timezone .utc )
244+
243245 if not usage and not reset_data :
244246 output_message ("Unable to fetch Codex data" , "Check your network connection and try the workflow again." )
245247 return
246248
247- now = datetime .now (timezone .utc )
248249 credits = reset_data .get ("credits" , []) if reset_data else []
249250 available_credits = [credit for credit in credits if is_available_credit (credit )]
250251
251252 available_count = 0
252- if reset_data :
253+ if reset_data is not None :
253254 available_count = int (reset_data .get ("available_count" , len (available_credits )) or 0 )
254255 elif usage :
255256 available_count = int (usage .get ("rate_limit_reset_credits" , {}).get ("available_count" , 0 ) or 0 )
0 commit comments