Skip to content

Your lightweight open-source personal agent.

refactor(triggers): clarify stored integer coercion

chengyongruffb7ddfa1e14
diff --git a/nanobot/triggers/local_types.py b/nanobot/triggers/local_types.py
index 49b1ddb28..f73439631 100644
--- a/nanobot/triggers/local_types.py
+++ b/nanobot/triggers/local_types.py
@@ -11,11 +11,9 @@ from nanobot.utils.dict_keys import get_camel_snake as _get
TriggerStatus = Literal["ok", "error"]
-def _store_int(value: Any, default: int = 0) -> int:
- """Coerce JSON numerics to int; treat null/blank like a missing key."""
- if value is None or value == "":
- return default
- return int(value)
+def _int_or_zero(value: Any) -> int:
+ """Coerce a stored JSON numeric, using zero for null or empty values."""
+ return 0 if value is None or value == "" else int(value)
@dataclass
@@ -29,7 +27,7 @@ class TriggerRunRecord:
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "TriggerRunRecord":
return cls(
- run_at_ms=_store_int(_get(data, "runAtMs", "run_at_ms", 0)),
+ run_at_ms=_int_or_zero(_get(data, "runAtMs", "run_at_ms", 0)),
status=str(data.get("status") or "error"), # type: ignore[arg-type]
error=data.get("error"),
)
@@ -77,8 +75,8 @@ class LocalTrigger:
session_key=str(_get(data, "sessionKey", "session_key", "")),
sender_id=str(_get(data, "senderId", "sender_id", "trigger") or "trigger"),
origin_metadata=dict(_get(data, "originMetadata", "origin_metadata", {}) or {}),
- created_at_ms=_store_int(_get(data, "createdAtMs", "created_at_ms", 0)),
- updated_at_ms=_store_int(_get(data, "updatedAtMs", "updated_at_ms", 0)),
+ created_at_ms=_int_or_zero(_get(data, "createdAtMs", "created_at_ms", 0)),
+ updated_at_ms=_int_or_zero(_get(data, "updatedAtMs", "updated_at_ms", 0)),
last_run_at_ms=_get(data, "lastRunAtMs", "last_run_at_ms"),
last_status=_get(data, "lastStatus", "last_status"), # type: ignore[arg-type]
last_error=_get(data, "lastError", "last_error"),
@@ -127,8 +125,8 @@ class TriggerDelivery:
id=str(data["id"]),
trigger_id=str(_get(data, "triggerId", "trigger_id", "")),
content=str(data.get("content") or ""),
- created_at_ms=_store_int(_get(data, "createdAtMs", "created_at_ms", 0)),
- attempts=_store_int(data.get("attempts", 0)),
+ created_at_ms=_int_or_zero(_get(data, "createdAtMs", "created_at_ms", 0)),
+ attempts=_int_or_zero(data.get("attempts", 0)),
last_error=data.get("lastError") or data.get("last_error"),
path=path,
)