fix(agent99): reconcile live cold-start dispatches
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 2m8s
CD Pipeline / build-and-deploy (push) Successful in 13m33s
CD Pipeline / post-deploy-checks (push) Has been cancelled

This commit is contained in:
ogt
2026-07-15 03:18:09 +08:00
parent 66aeb34bf6
commit 29072e4004
11 changed files with 300 additions and 73 deletions

View File

@@ -343,6 +343,15 @@ def build_agent99_dispatch_receipt_envelope(
else "dispatch_delivery_unknown_reconcile_only"
),
"identity": public_identity,
"dispatch_scope": {
"schema_version": "agent99_dispatch_scope_v1",
"kind": str(dispatch_receipt.get("kind") or ""),
"suggested_mode": str(dispatch_receipt.get("suggested_mode") or ""),
"target_resource": str(dispatch_receipt.get("target_resource") or ""),
"controlled_apply_requested": bool(
dispatch_receipt.get("controlled_apply_requested") is True
),
},
"dispatch_receipt": {
"schema_version": str(
dispatch_receipt.get("schema_version")
@@ -493,6 +502,13 @@ class PostgresAgent99DispatchLedger:
"schema_version": "agent99_controlled_dispatch_receipt_v1",
"status": "dispatch_reserved",
"identity": identity_payload,
"dispatch_scope": {
"schema_version": "agent99_dispatch_scope_v1",
"kind": input_payload["kind"],
"suggested_mode": input_payload["suggested_mode"],
"target_resource": input_payload["target_resource"],
"controlled_apply_requested": input_payload["controlled_apply"],
},
"dispatch_accepted": False,
"controlled_apply_requested": bool(input_payload["controlled_apply"]),
"controlled_apply_authorized": False,

View File

@@ -13,6 +13,7 @@ from __future__ import annotations
import json
import math
import re
import time
import urllib.error
import urllib.parse
@@ -82,16 +83,51 @@ _COLD_START_QUERIES = {
}
def is_cold_start_same_run_identity(identity: Agent99DispatchIdentity) -> bool:
"""Limit this transport to the existing cold-start Recover identity."""
def is_cold_start_same_run_identity(
identity: Agent99DispatchIdentity,
receipt: dict[str, Any] | None = None,
) -> bool:
"""Admit only a canonical cold-start Recover dispatch to no-write Status."""
return bool(
legacy_identity = bool(
identity.route_id == AGENT99_COLD_START_ROUTE_ID
and identity.incident_id == AGENT99_COLD_START_INCIDENT_ID
and str(identity.run_id) == AGENT99_COLD_START_RUN_ID
and identity.source_fingerprint.startswith("legacy-cold-start:")
and identity.execution_generation == "1"
)
if legacy_identity:
return True
scope = (
receipt.get("dispatch_scope")
if isinstance(receipt, dict)
and isinstance(receipt.get("dispatch_scope"), dict)
else {}
)
try:
generation = int(identity.execution_generation)
except ValueError:
return False
expected_work_item_id = (
f"agent99-dispatch:awoooi:{identity.incident_id}:"
f"{AGENT99_COLD_START_ROUTE_ID}"
)
return bool(
identity.project_id == "awoooi"
and identity.route_id == AGENT99_COLD_START_ROUTE_ID
and re.fullmatch(r"INC-[0-9]{8}-[0-9A-F]{6}", identity.incident_id)
is not None
and identity.work_item_id == expected_work_item_id
and re.fullmatch(r"[0-9a-f]{32,64}", identity.source_fingerprint)
is not None
and 1 <= generation <= 3
and scope.get("schema_version") == "agent99_dispatch_scope_v1"
and scope.get("kind") == "host_recovery"
and scope.get("suggested_mode") == "Recover"
and scope.get("target_resource") == "cold-start-gate"
and scope.get("controlled_apply_requested") is True
)
def agent99_same_run_evidence_id(identity: Agent99DispatchIdentity) -> str:
@@ -235,6 +271,7 @@ def read_cold_start_source_resolution(
def request_agent99_same_run_status_reconcile(
identity: Agent99DispatchIdentity,
source_receipt: dict[str, Any],
dispatch_receipt: dict[str, Any] | None = None,
*,
relay_url: str | None = None,
relay_token: str | None = None,
@@ -242,7 +279,7 @@ def request_agent99_same_run_status_reconcile(
) -> dict[str, Any]:
"""Ask Agent99 for one idempotent, no-write Status on the existing run."""
if not is_cold_start_same_run_identity(identity):
if not is_cold_start_same_run_identity(identity, dispatch_receipt):
return {
"status": "identity_not_eligible_fail_closed",
"accepted": False,

View File

@@ -838,6 +838,14 @@ def dispatch_agent99_sre_alert_with_receipt(
alert_id = _safe_text(payload.get("id"), max_length=160)
kind = _safe_text(payload.get("kind"), max_length=120)
awoooi = (
payload.get("awoooi")
if isinstance(payload.get("awoooi"), dict)
else {}
)
target_resource = _safe_text(awoooi.get("targetResource"), max_length=240)
suggested_mode = _safe_text(payload.get("suggestedMode"), max_length=80)
controlled_apply_requested = bool(payload.get("controlledApply") is True)
if settings.AGENT99_SRE_ALERT_RELAY_URL:
raw_receipt = post_agent99_sre_alert(payload) or {}
response_payload: dict[str, Any] = {}
@@ -862,6 +870,9 @@ def dispatch_agent99_sre_alert_with_receipt(
"transport": "relay",
"alert_id": alert_id,
"kind": kind,
"target_resource": target_resource,
"suggested_mode": suggested_mode,
"controlled_apply_requested": controlled_apply_requested,
"http_status": http_status,
"accepted": accepted,
"inbox_triggered": inbox_triggered,
@@ -884,6 +895,9 @@ def dispatch_agent99_sre_alert_with_receipt(
"transport": "file",
"alert_id": alert_id,
"kind": kind,
"target_resource": target_resource,
"suggested_mode": suggested_mode,
"controlled_apply_requested": controlled_apply_requested,
"accepted": True,
"inbox_triggered": False,
# A file write is not proof that the Agent99 inbox consumed it.
@@ -903,6 +917,9 @@ def dispatch_agent99_sre_alert_with_receipt(
"transport": "disabled",
"alert_id": alert_id,
"kind": kind,
"target_resource": target_resource,
"suggested_mode": suggested_mode,
"controlled_apply_requested": controlled_apply_requested,
"accepted": False,
"inbox_triggered": False,
"delivery_certainty": "not_delivered",