Ограничить повторные уведомления и проверять восстановление в заданный срок

This commit is contained in:
2026-11-27 12:18:55 +03:00
parent b9b6ece535
commit 0076659224
5 changed files with 28464 additions and 8 deletions
+52 -6
View File
@@ -31,6 +31,36 @@ def images():
}
def observe_get(client, path, *, deadline, observations, kind, **kwargs):
"""Observe recovery without mistaking a transient read failure for loss.
Every error remains counted. Authentication/ACL/contract failures are fatal,
and an unavailable object never becomes a successful observation by retrying.
The caller still has to prove every acknowledgement and ready attachment.
"""
counts = observations.setdefault(kind, {})
while (remaining := deadline - time.monotonic()) > 0:
try:
response = client.get(path, timeout=min(8, remaining), **kwargs)
except httpx.TransportError as error:
key = "transport:" + type(error).__name__
else:
key = str(response.status_code)
counts[key] = counts.get(key, 0) + 1
if response.status_code == 200:
return response
if response.status_code not in {429, 502, 503, 504}:
raise AssertionError(
f"Unexpected {kind} observation status {response.status_code}"
)
# No tokens, response bodies or private request headers in evidence.
time.sleep(min(0.25, max(0, deadline - time.monotonic())))
continue
counts[key] = counts.get(key, 0) + 1
time.sleep(min(0.25, max(0, deadline - time.monotonic())))
raise TimeoutError(f"Recovery observation deadline exceeded: {kind}")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", default="evidence/pki-research-first.json")
@@ -302,16 +332,21 @@ def main():
# counted separately and never presented as acknowledged writes.
messages = {row[5] for row in load.requests if row[2] == 201}
found, cursor = set(), None
observations = report["preservation_observations"] = {}
message_deadline = time.monotonic() + 180
while True:
r = client.get(
r = observe_get(
client,
f"/api/v1/chats/{chat}/messages",
deadline=message_deadline,
observations=observations,
kind="message_pages",
headers=headers,
params={
"limit": 100,
**({"cursor": cursor} if cursor else {}),
},
)
assert r.status_code == 200
page = r.json()
found.update(item["id"] for item in page["items"])
cursor = page["next_cursor"]
@@ -333,10 +368,14 @@ def main():
failed = set()
while pending and time.monotonic() < deadline:
for identity in list(pending):
r = client.get(
"/api/v1/attachments/" + identity, headers=headers
r = observe_get(
client,
"/api/v1/attachments/" + identity,
deadline=deadline,
observations=observations,
kind="attachment_metadata",
headers=headers,
)
assert r.status_code == 200
state = r.json()["status"]
if state == "ready":
pending.remove(identity)
@@ -365,15 +404,22 @@ def main():
"PKI research recovery after controlled seal",
)
failed.clear()
report["drain"]["remaining"] = len(pending)
save()
time.sleep(1)
report["drain"].update(
elapsed_seconds=round(time.monotonic() - drain_started, 3),
remaining=len(pending),
)
assert not pending, "Accepted upload not ready after recovery"
content_deadline = time.monotonic() + 120
for row in accepted[:: max(1, len(accepted) // 5)]:
r = client.get(
r = observe_get(
client,
"/api/v1/attachments/" + row[3] + "/content",
deadline=content_deadline,
observations=observations,
kind="sampled_originals",
headers=headers,
)
assert r.status_code == 200 and r.content == load.raw