fix: Agent survives slow Hub on large syncs (25-product chunks, 120s timeout, retry) and keeps heartbeating during jobs; 1.1.4

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
gti
2026-09-24 09:33:54 -03:00
co-authored by Claude Sonnet 5
parent d1104589cc
commit e40e9500a4
19 changed files with 75 additions and 26 deletions
+25 -4
View File
@@ -20,6 +20,7 @@ import logging.handlers
import os
import signal
import sys
import threading
import time
from pathlib import Path
@@ -102,7 +103,7 @@ class Config:
# (A legacy AGENT_JOBS_ENABLED line in an old agent.env is deliberately ignored.)
self.agent_jobs_enabled = True
self.poll_interval_seconds = float(env.get("POLL_INTERVAL_SECONDS", "5"))
self.agent_version = env.get("AGENT_VERSION", "1.1.3")
self.agent_version = env.get("AGENT_VERSION", "1.1.4")
self.schema_fingerprint = env.get("SCHEMA_FINGERPRINT", REFERENCE_SCHEMA_V1)
self.state_db = env.get("AGENT_STATE_DB", str(AGENT_ROOT / "agent_state.sqlite"))
# Pilot write authorization: both must be explicitly set by the installer for
@@ -239,7 +240,7 @@ def execute_job(job, config, executor, state, env):
"coverage": coverage,
"operational": operational,
"fiscal_mode": getattr(config, "fiscal_mode", None),
"release_version": env.get("AGENT_VERSION") or "1.1.3",
"release_version": env.get("AGENT_VERSION") or "1.1.4",
"products_total": len(products),
"products_changed": len(to_process),
"products_processed": summary["processed"],
@@ -268,6 +269,17 @@ def execute_job(job, config, executor, state, env):
return None, f"{type(error).__name__}: {error}"
BUSY_HEARTBEAT_SECONDS = 20
def _busy_heartbeats(client, installation_id, config, job_id, done):
while not done.wait(BUSY_HEARTBEAT_SECONDS):
try:
client.heartbeat_metadata({"installation_id": installation_id, "status": "BUSY", "schema_fingerprint": config.schema_fingerprint, "agent_version": config.agent_version, "last_job_id": job_id})
except Exception: # noqa: BLE001 — Hub hiccup mid-job must not affect the job
pass
def resolve_installation_id(config, env, state, client):
"""Recover and persist the Hub-bound identity; never invent one for an Agent."""
persisted = state.stored_installation_id()
@@ -362,7 +374,16 @@ def run(config, env, *, max_iterations=None, stopper=None):
except Exception:
pass # best-effort; the job itself still proceeds
result_summary, error = execute_job(job, config, executor, state, env)
# A full sync can take minutes: keep the heartbeat alive (BUSY) from a side thread so the
# panel never shows a working Agent as OFFLINE. The side thread only talks to the Hub
# (no SQLite/Firebird), so it cannot interfere with the job.
done = threading.Event()
beat = threading.Thread(target=_busy_heartbeats, args=(client, installation_id, config, last_job_id, done), daemon=True)
beat.start()
try:
result_summary, error = execute_job(job, config, executor, state, env)
finally:
done.set()
try:
if error is None:
client.complete_job(job.get("job_id"), result_summary)
@@ -377,7 +398,7 @@ def run(config, env, *, max_iterations=None, stopper=None):
def enforce_startup_preflight(env):
"""Full read-only preflight at startup (Hub, auth, Firebird, capabilities, company context, guards). Fail closed."""
from gti_fiscal_agent.preflight import run_preflight
report = run_preflight(env, agent_version=env.get("AGENT_VERSION") or "1.1.3")
report = run_preflight(env, agent_version=env.get("AGENT_VERSION") or "1.1.4")
logger.info("startup preflight: %s", json.dumps({k: report[k] for k in ("PREFLIGHT", "blockers", "fiscal_mode", "effective_firebird_writes")}, ensure_ascii=False))
print(json.dumps({"PREFLIGHT": report["PREFLIGHT"], "blockers": report["blockers"]}, ensure_ascii=False))
if report["PREFLIGHT"] != "PASS":