HEL-835: guard v3 - done-delta streams settle ok on early close; 36/36
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
co-authored by
multica-agent
parent
b98ab6ab50
commit
65df25271e
+15
-3
@@ -168,21 +168,33 @@ class SceneBoundProvider:
|
|||||||
self.scene = scene
|
self.scene = scene
|
||||||
|
|
||||||
async def stream(self, messages, **kwargs):
|
async def stream(self, messages, **kwargs):
|
||||||
"""Async generator like the wrapped provider; reserves on first pull."""
|
"""Async generator like the wrapped provider; reserves on first pull.
|
||||||
|
|
||||||
|
Settlement semantics: seeing a done delta (or stop finish) before the
|
||||||
|
consumer closes means the upstream call actually completed, so an early
|
||||||
|
close settles "ok", not "cancelled".
|
||||||
|
"""
|
||||||
category = SCENE_CATEGORY.get(self.scene)
|
category = SCENE_CATEGORY.get(self.scene)
|
||||||
entry = self.ledger.reserve(category)
|
entry = self.ledger.reserve(category)
|
||||||
log.info("hel835 guard call scene=%s category=%s", self.scene.value, category)
|
log.info("hel835 guard call scene=%s category=%s", self.scene.value, category)
|
||||||
upstream = self.real.stream(messages, **kwargs)
|
upstream = self.real.stream(messages, **kwargs)
|
||||||
|
saw_done = False
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
delta = await upstream.__anext__()
|
delta = await upstream.__anext__()
|
||||||
|
if delta.done or getattr(delta, "finish_reason", None) == "stop":
|
||||||
|
saw_done = True
|
||||||
yield delta
|
yield delta
|
||||||
# pragma: no cover - while True never falls through
|
# pragma: no cover - while True never falls through
|
||||||
except StopAsyncIteration:
|
except StopAsyncIteration:
|
||||||
self.ledger.settle(entry, "ok")
|
self.ledger.settle(entry, "ok")
|
||||||
except BaseException as exc:
|
except BaseException as exc:
|
||||||
outcome = "cancelled" if isinstance(exc, (asyncio.CancelledError, GeneratorExit)) else "fail"
|
if isinstance(exc, asyncio.CancelledError):
|
||||||
self.ledger.settle(entry, outcome, repr(exc))
|
self.ledger.settle(entry, "ok" if saw_done else "cancelled", repr(exc))
|
||||||
|
elif isinstance(exc, GeneratorExit):
|
||||||
|
self.ledger.settle(entry, "ok" if saw_done else "cancelled")
|
||||||
|
else:
|
||||||
|
self.ledger.settle(entry, "fail", repr(exc))
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
|
|||||||
@@ -191,6 +191,20 @@ async def main():
|
|||||||
counts4 = json.loads(p4.read_text())["counts"]
|
counts4 = json.loads(p4.read_text())["counts"]
|
||||||
check("R4 cancelled attempt still counted", counts4["dialogue_main"] == 1, str(counts4))
|
check("R4 cancelled attempt still counted", counts4["dialogue_main"] == 1, str(counts4))
|
||||||
|
|
||||||
|
# R4b: a stream that saw done then gets closed by the consumer settles ok
|
||||||
|
# (real app/structured paths break after the done delta and aclose).
|
||||||
|
done_probe = ProbeLLM()
|
||||||
|
router4b = GuardedRouter(providers={t: done_probe for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)},
|
||||||
|
routing={}, guard=None, ledger=GuardLedger(p4))
|
||||||
|
gen = router4b.route(Scene.MEMORY_GIST).provider.stream(chat_msgs())
|
||||||
|
async with aclosing(gen):
|
||||||
|
async for delta in gen:
|
||||||
|
if delta.done:
|
||||||
|
break
|
||||||
|
calls4 = json.loads(p4.read_text())["calls"]
|
||||||
|
check("R4b done-then-close settles ok", calls4[-1]["outcome"] == "ok", str(calls4[-1]))
|
||||||
|
check("R4b upstream closed once", done_probe.closed == 1)
|
||||||
|
|
||||||
# ---------- v1 regression set (through the scene-bound router)
|
# ---------- v1 regression set (through the scene-bound router)
|
||||||
p5 = full_ledger(tmp, "r5.json", memory_gist=4, dialogue_main=2, persona_generation=1,
|
p5 = full_ledger(tmp, "r5.json", memory_gist=4, dialogue_main=2, persona_generation=1,
|
||||||
growth_extract=2)
|
growth_extract=2)
|
||||||
|
|||||||
@@ -14,16 +14,18 @@ PASS R3 missing capped keys refuse start
|
|||||||
PASS R3 negative counter refuses start
|
PASS R3 negative counter refuses start
|
||||||
PASS R3 valid audited ledger loads
|
PASS R3 valid audited ledger loads
|
||||||
PASS R3 {} ledger cannot re-reserve persona
|
PASS R3 {} ledger cannot re-reserve persona
|
||||||
PASS R4 early close settles cancelled [{'call_id': '1792824362169132157-315737-a887b1fc', 't': '2026-10-24T06:46:02Z', 'category': 'dialogue_main', 'pid': 315737, 'outcome': 'cancelled', 'detail': 'GeneratorExit()'}]
|
PASS R4 early close settles cancelled [{'call_id': '1792890182152408199-691221-b12ccb72', 't': '2026-10-25T01:03:02Z', 'category': 'dialogue_main', 'pid': 691221, 'outcome': 'cancelled'}]
|
||||||
PASS R4 upstream explicitly closed closed=1
|
PASS R4 upstream explicitly closed closed=1
|
||||||
PASS R4 cancelled attempt still counted {'memory_gist': 0, 'dialogue_main': 1, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
PASS R4 cancelled attempt still counted {'memory_gist': 0, 'dialogue_main': 1, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
||||||
|
PASS R4b done-then-close settles ok {'call_id': '1792890182253008414-691221-79830ad2', 't': '2026-10-25T01:03:02Z', 'category': 'memory_gist', 'pid': 691221, 'outcome': 'ok'}
|
||||||
|
PASS R4b upstream closed once
|
||||||
PASS v1 persona 1/1 refuses rerun
|
PASS v1 persona 1/1 refuses rerun
|
||||||
PASS v1 persona refusal zero upstream calls
|
PASS v1 persona refusal zero upstream calls
|
||||||
PASS v1 gist cumulative to cap 8 {'memory_gist': 8, 'dialogue_main': 2, 'persona_generation': 1, 'growth_extract': 2, 'life_event': 0, 'sharing_judge': 0}
|
PASS v1 gist cumulative to cap 8 {'memory_gist': 8, 'dialogue_main': 2, 'persona_generation': 1, 'growth_extract': 2, 'life_event': 0, 'sharing_judge': 0}
|
||||||
PASS v1 9th gist refused
|
PASS v1 9th gist refused
|
||||||
PASS v1 exactly 4 upstream gist calls
|
PASS v1 exactly 4 upstream gist calls
|
||||||
PASS v1 restart resumes account
|
PASS v1 restart resumes account
|
||||||
PASS v1 failed call counted and settled fail {'call_id': '1792824362272415743-315737-d88b3a6b', 't': '2026-10-24T06:46:02Z', 'category': 'memory_gist', 'pid': 315737, 'outcome': 'fail', 'detail': "OSError('controlled upstream failure')"}
|
PASS v1 failed call counted and settled fail {'call_id': '1792890182254928947-691221-d603a9f7', 't': '2026-10-25T01:03:02Z', 'category': 'memory_gist', 'pid': 691221, 'outcome': 'fail', 'detail': "OSError('controlled upstream failure')"}
|
||||||
PASS v1 after failure cap holds
|
PASS v1 after failure cap holds
|
||||||
PASS v1 concurrent ledger never exceeds 8 {'memory_gist': 8, 'dialogue_main': 0, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
PASS v1 concurrent ledger never exceeds 8 {'memory_gist': 8, 'dialogue_main': 0, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
||||||
PASS v1 concurrent exactly 4 served 6 refused ['ok', 'ok', 'ok', 'ok', 'cap', 'cap', 'cap', 'cap', 'cap', 'cap']
|
PASS v1 concurrent exactly 4 served 6 refused ['ok', 'ok', 'ok', 'ok', 'cap', 'cap', 'cap', 'cap', 'cap', 'cap']
|
||||||
@@ -35,4 +37,4 @@ PASS v1 missing ledger refuses start
|
|||||||
PASS assembly poisoned-boundary chat counts dialogue_main only {'memory_gist': 0, 'dialogue_main': 1, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
PASS assembly poisoned-boundary chat counts dialogue_main only {'memory_gist': 0, 'dialogue_main': 1, 'persona_generation': 0, 'growth_extract': 0, 'life_event': 0, 'sharing_judge': 0}
|
||||||
PASS assembly health ok with guard
|
PASS assembly health ok with guard
|
||||||
|
|
||||||
34 passed, 0 failed
|
36 passed, 0 failed
|
||||||
|
|||||||
Reference in New Issue
Block a user