Skip to content

Commit ba75ab9

Browse files
feat(winrate): add ConfirmationStore.handle API (pending/expired/confirmed)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 9dc713b commit ba75ab9

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

src/utils/winrate_upgrade.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,54 @@ def pop_if_confirmed(self, key: str, delay: int, ttl: int) -> Tuple[bool, Option
6262
return True, payload
6363
return False, None
6464

65+
def handle(self, key: str, delay: int, ttl: int, payload: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
66+
"""
67+
High-level helper that implements the desired handle() API:
68+
- If no entry exists: mark pending and return {"status":"pending"}
69+
- If entry exists but expired: remove and return {"status":"expired"}
70+
- If entry exists and delay has passed: remove and return {"status":"confirmed", "payload": ...}
71+
- If entry exists and still within delay: return {"status":"pending", "seconds_seen": ...}
72+
"""
73+
now = time.time()
74+
with self.lock:
75+
entry = self._data.get(key)
76+
if not entry:
77+
# create pending entry
78+
self._data[key] = {"first_seen": now, "payload": payload or {}}
79+
try:
80+
self._save()
81+
except Exception:
82+
pass
83+
return {"status": "pending", "first_seen": now}
84+
85+
first = entry.get("first_seen", now)
86+
age = now - first
87+
if age > ttl:
88+
# expired
89+
try:
90+
del self._data[key]
91+
except KeyError:
92+
pass
93+
try:
94+
self._save()
95+
except Exception:
96+
pass
97+
return {"status": "expired", "age": age}
98+
99+
if age >= delay:
100+
payload_out = entry.get("payload")
101+
try:
102+
del self._data[key]
103+
except KeyError:
104+
pass
105+
try:
106+
self._save()
107+
except Exception:
108+
pass
109+
return {"status": "confirmed", "payload": payload_out, "age": age}
110+
111+
return {"status": "pending", "age": age}
112+
65113
def expire_all_older_than(self, ttl: int) -> int:
66114
removed = 0
67115
now = time.time()

0 commit comments

Comments
 (0)