Skip to content

Commit 8ad384a

Browse files
committed
fix(chat): run expiring after 10 minutes
1 parent e4bca18 commit 8ad384a

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/askui/chat/api/runs/models.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, timedelta, timezone
1+
from datetime import timedelta
22
from typing import Literal
33

44
from pydantic import BaseModel, computed_field
@@ -64,7 +64,7 @@ def create(cls, thread_id: ThreadId, params: RunCreateParams) -> "Run":
6464
id=generate_time_ordered_id("run"),
6565
thread_id=thread_id,
6666
created_at=now(),
67-
expires_at=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
67+
expires_at=now() + timedelta(minutes=10),
6868
**params.model_dump(exclude={"stream"}),
6969
)
7070

@@ -77,10 +77,27 @@ def status(self) -> RunStatus:
7777
return "failed"
7878
if self.completed_at:
7979
return "completed"
80-
if self.expires_at and self.expires_at < datetime.now(tz=timezone.utc):
80+
if self.expires_at and self.expires_at < now():
8181
return "expired"
8282
if self.tried_cancelling_at:
8383
return "cancelling"
8484
if self.started_at:
8585
return "in_progress"
8686
return "queued"
87+
88+
def start(self) -> None:
89+
self.started_at = now()
90+
self.expires_at = now() + timedelta(minutes=10)
91+
92+
def ping(self) -> None:
93+
self.expires_at = now() + timedelta(minutes=10)
94+
95+
def complete(self) -> None:
96+
self.completed_at = now()
97+
98+
def cancel(self) -> None:
99+
self.cancelled_at = now()
100+
101+
def fail(self, error: RunError) -> None:
102+
self.failed_at = now()
103+
self.last_error = error

src/askui/chat/api/runs/runner/runner.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
from abc import ABC, abstractmethod
3-
from datetime import datetime, timezone
43
from typing import TYPE_CHECKING, Literal, Sequence
54

65
import anthropic
@@ -142,6 +141,8 @@ async def _run_human_agent(self, send_stream: ObjectStream[Events]) -> None:
142141
updated_run = self._retrieve()
143142
if self._should_abort(updated_run):
144143
break
144+
updated_run.ping()
145+
self._run_service.save(updated_run)
145146
while event := self._agent_os.poll_event():
146147
if self._should_abort(updated_run):
147148
break
@@ -287,6 +288,8 @@ async def async_on_message(
287288
updated_run = self._retrieve()
288289
if self._should_abort(updated_run):
289290
return None
291+
updated_run.ping()
292+
self._run_service.save(updated_run)
290293
return on_message_cb_param.message
291294

292295
on_message = syncify(async_on_message)
@@ -390,7 +393,7 @@ async def run(
390393
)
391394
updated_run = self._retrieve()
392395
if updated_run.status == "in_progress":
393-
updated_run.completed_at = datetime.now(tz=timezone.utc)
396+
updated_run.complete()
394397
self._run_service.save(updated_run)
395398
await send_stream.send(
396399
RunEvent(
@@ -405,7 +408,7 @@ async def run(
405408
event="thread.run.cancelling",
406409
)
407410
)
408-
updated_run.cancelled_at = datetime.now(tz=timezone.utc)
411+
updated_run.cancel()
409412
self._run_service.save(updated_run)
410413
await send_stream.send(
411414
RunEvent(
@@ -424,8 +427,7 @@ async def run(
424427
except Exception as e: # noqa: BLE001
425428
logger.exception("Exception in runner")
426429
updated_run = self._retrieve()
427-
updated_run.failed_at = datetime.now(tz=timezone.utc)
428-
updated_run.last_error = RunError(message=str(e), code="server_error")
430+
updated_run.fail(RunError(message=str(e), code="server_error"))
429431
self._run_service.save(updated_run)
430432
await send_stream.send(
431433
RunEvent(
@@ -440,7 +442,7 @@ async def run(
440442
)
441443

442444
def _mark_run_as_started(self) -> None:
443-
self._run.started_at = datetime.now(tz=timezone.utc)
445+
self._run.start()
444446
self._run_service.save(self._run)
445447

446448
def _should_abort(self, run: Run) -> bool:

0 commit comments

Comments
 (0)