fix: state.db 时间戳单位自适应(秒/毫秒混存)+ default profile 无sessions表静默跳过

This commit is contained in:
hmo
2026-07-21 01:54:59 +08:00
parent 839c6fc2ff
commit 2054c6e6f9
2 changed files with 16 additions and 3 deletions
@@ -59,6 +59,13 @@ def save_state(st):
pass
def _to_ms(ts):
"""started_at 单位自适应:>1e12 视为毫秒,否则视为秒。"""
if not ts:
return 0
return ts if ts > 1e12 else ts * 1000
def main():
st = load_state()
alerted = set(st.get("alerted", []))
@@ -70,6 +77,10 @@ def main():
try:
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=10)
conn.row_factory = sqlite3.Row
tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
if "sessions" not in tables:
conn.close()
continue
rows = conn.execute("""
SELECT id, started_at, message_count, input_tokens, tool_call_count
FROM sessions
@@ -83,7 +94,7 @@ def main():
continue
for r in rows:
sa = r["started_at"] or 0
sa = _to_ms(r["started_at"])
age_sec = (now_ms - sa) / 1000
if age_sec < MIN_AGE_SEC:
continue
@@ -216,7 +216,8 @@ def check_stale_sessions():
except Exception:
continue
for r in rows:
sa = (r['started_at'] or 0) / 1000
raw_sa = r['started_at'] or 0
sa = (raw_sa if raw_sa > 1e12 else raw_sa * 1000) / 1000 # 单位自适应
if not (0 < sa < soul_mtime):
continue
# 只报"仍活跃"的:最近 6 小时内有消息(已被 bump 遗弃的旧 session 不报)
@@ -226,7 +227,8 @@ def check_stale_sessions():
"SELECT MAX(created_at) FROM messages WHERE session_id=?",
(r['id'],)).fetchone()[0]
conn2.close()
last_ts = (last_msg or 0) / 1000
lm = last_msg or 0
last_ts = (lm if lm > 1e12 else lm * 1000) / 1000 # 单位自适应
if last_ts and (datetime.now().timestamp() - last_ts) > 6 * 3600:
continue
except Exception: