Compare commits

..

1 Commits

Author SHA1 Message Date
64da98c2bf Add exponential backoff retry logic to GiteaClient 2026-04-06 17:44:07 +00:00
2 changed files with 34 additions and 57 deletions

View File

@@ -19,6 +19,7 @@ import os
import urllib.request
import urllib.error
import urllib.parse
import time
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
@@ -211,37 +212,53 @@ class GiteaClient:
# -- HTTP layer ----------------------------------------------------------
def _request(
self,
method: str,
path: str,
data: Optional[dict] = None,
params: Optional[dict] = None,
retries: int = 3,
backoff: float = 1.5,
) -> Any:
"""Make an authenticated API request. Returns parsed JSON."""
"""Make an authenticated API request with exponential backoff retries."""
url = f"{self.api}{path}"
if params:
url += "?" + urllib.parse.urlencode(params)
body = json.dumps(data).encode() if data else None
req = urllib.request.Request(url, data=body, method=method)
req.add_header("Authorization", f"token {self.token}")
req.add_header("Content-Type", "application/json")
req.add_header("Accept", "application/json")
for attempt in range(retries):
req = urllib.request.Request(url, data=body, method=method)
req.add_header("Authorization", f"token {self.token}")
req.add_header("Content-Type", "application/json")
req.add_header("Accept", "application/json")
try:
with urllib.request.urlopen(req, timeout=30) as resp:
raw = resp.read().decode()
if not raw:
return {}
return json.loads(raw)
except urllib.error.HTTPError as e:
body_text = ""
try:
body_text = e.read().decode()
except Exception:
pass
raise GiteaError(e.code, body_text, url) from e
with urllib.request.urlopen(req, timeout=30) as resp:
raw = resp.read().decode()
if not raw:
return {}
return json.loads(raw)
except urllib.error.HTTPError as e:
# Don't retry client errors (4xx) except 429
if 400 <= e.code < 500 and e.code != 429:
body_text = ""
try:
body_text = e.read().decode()
except Exception:
pass
raise GiteaError(e.code, body_text, url) from e
if attempt == retries - 1:
raise GiteaError(e.code, str(e), url) from e
time.sleep(backoff ** attempt)
except (urllib.error.URLError, TimeoutError) as e:
if attempt == retries - 1:
raise GiteaError(500, str(e), url) from e
time.sleep(backoff ** attempt)
def _get(self, path: str, **params) -> Any:
# Filter out None values

View File

@@ -45,46 +45,6 @@ def newest_file(directory, pattern):
files = sorted(directory.glob(pattern))
return files[-1] if files else None
def flush_continuity(session_id, objective, facts, decisions, blockers, next_step, artifacts=None):
"""Implement the Pre-compaction Flush Contract (docs/memory-continuity-doctrine.md).
Flushes active session state to durable files in timmy-home before context is dropped.
"""
now = datetime.now(timezone.utc)
today_str = now.strftime("%Y-%m-%d")
# 1. Daily log append
daily_log = TIMMY_HOME / "daily-notes" / f"{today_str}.md"
daily_log.parent.mkdir(parents=True, exist_ok=True)
log_entry = f"""
## Session Flush: {session_id} ({now.isoformat()})
- **Objective**: {objective}
- **Facts Learned**: {", ".join(facts) if facts else "none"}
- **Decisions**: {", ".join(decisions) if decisions else "none"}
- **Blockers**: {", ".join(blockers) if blockers else "none"}
- **Next Step**: {next_step}
- **Artifacts**: {", ".join(artifacts) if artifacts else "none"}
---
"""
with open(daily_log, "a") as f:
f.write(log_entry)
# 2. Session handoff update
handoff_file = TIMMY_HOME / "continuity" / "active.md"
handoff_file.parent.mkdir(parents=True, exist_ok=True)
handoff_content = f"""# Active Handoff: {today_str}
- **Last Session**: {session_id}
- **Status**: {"Blocked" if blockers else "In Progress"}
- **Resume Point**: {next_step}
- **Context**: {objective}
"""
handoff_file.write_text(handoff_content)
return {"status": "flushed", "path": str(daily_log)}
def run_hermes_local(
prompt,
model=None,