fix(r2): keep her self-narration and life within the approved room
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -7,6 +7,7 @@ import asyncio
|
||||
import json
|
||||
import tempfile
|
||||
import time
|
||||
from contextlib import aclosing
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -18,9 +19,42 @@ from cybergril.life.persona import Persona, fingerprint
|
||||
from cybergril.life.runtime import LifeConfig, LifeRuntime
|
||||
|
||||
|
||||
def record_provider(provider, report, output):
|
||||
original = provider.stream
|
||||
|
||||
async def stream(messages, **kwargs):
|
||||
if len(report["calls"]) >= 4:
|
||||
raise RuntimeError("four-call hard limit reached")
|
||||
call = {"number": len(report["calls"]) + 1, "model": provider.model,
|
||||
"base_url": provider.base_url,
|
||||
"messages": [{"role": m.role, "content": m.content} for m in messages],
|
||||
"parameters": kwargs, "deltas": [], "raw_text": "", "completed": False}
|
||||
report["calls"].append(call)
|
||||
report["attempted_model_calls"] = len(report["calls"])
|
||||
# Count and persist the attempt before sending, including failures. No auth headers.
|
||||
output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
try:
|
||||
async with aclosing(original(messages, **kwargs)) as upstream:
|
||||
async for delta in upstream:
|
||||
call["deltas"].append({"text": delta.text, "done": delta.done,
|
||||
"finish_reason": delta.finish_reason, "usage": delta.usage})
|
||||
call["raw_text"] += delta.text
|
||||
call["completed"] |= delta.done or delta.finish_reason == "stop"
|
||||
yield delta
|
||||
except BaseException as exc:
|
||||
call["error_type"] = type(exc).__name__
|
||||
raise
|
||||
finally:
|
||||
output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
|
||||
provider.stream = stream
|
||||
|
||||
|
||||
async def run(output):
|
||||
if output.exists():
|
||||
raise FileExistsError("Refusing to overwrite evidence or repeat this run")
|
||||
report = {"synthetic_only": True, "browser_connected": False, "three_day_run": False,
|
||||
"max_model_calls": 4, "checks": {}}
|
||||
"max_model_calls": 4, "calls": [], "checks": {}}
|
||||
with tempfile.TemporaryDirectory(prefix="r2-smoke-", dir=output.parent) as directory:
|
||||
app = create_app(db_path=Path(directory) / "synthetic.db", life_config=LifeConfig(enabled=False))
|
||||
async with app.router.lifespan_context(app):
|
||||
@@ -29,17 +63,25 @@ async def run(output):
|
||||
response.raise_for_status()
|
||||
s, original = app.state.store, fingerprint(app.state.store)
|
||||
router, models = app.state.life.router, app.state.life.models
|
||||
for provider in set(router.provider_for(t) for t in router.available_tiers()):
|
||||
record_provider(provider, report, output)
|
||||
router.guard = PersistentBudget(s, router.guard)
|
||||
persona = Persona(s, router, models, enabled=True)
|
||||
start = time.monotonic()
|
||||
report["checks"]["persona"] = await persona.ensure()
|
||||
report["persona"] = persona.view()
|
||||
if not report["checks"]["persona"]:
|
||||
report["stopped"] = "persona_failed_no_retry"
|
||||
output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
return False
|
||||
runtime = LifeRuntime(s, router, models, LifeConfig(enabled=True), app.state.sharing, persona)
|
||||
# An explicit synthetic current-window invocation, not backfilled production history.
|
||||
await runtime.generate("synthetic-smoke", 0)
|
||||
life = s.query_one("SELECT * FROM life_event")
|
||||
report["checks"]["life"] = bool(life)
|
||||
report["life"] = dict(life) if life else None
|
||||
if not life:
|
||||
report["stopped"] = "life_failed_no_retry"
|
||||
if life:
|
||||
for name, text in (
|
||||
("narrated", "刚才我这边发生了一件小事:" + life["content"]),
|
||||
@@ -54,11 +96,15 @@ async def run(output):
|
||||
judged = dict(s.query_one("SELECT state,selected FROM sharing_attempt WHERE message_id=?", (receipt["message_id"],)))
|
||||
report[name] = judged
|
||||
report["checks"][name] = judged["state"] == "judged" and json.loads(judged["selected"]) == ([life["id"]] if name == "narrated" else [])
|
||||
if not report["checks"][name]:
|
||||
report["stopped"] = name + "_failed_no_retry"
|
||||
break
|
||||
report["checks"]["unacknowledged_hidden"] = not s.query("SELECT * FROM life_event WHERE told=1")
|
||||
report["checks"]["birth_unchanged"] = fingerprint(s) == original
|
||||
report["elapsed_seconds"] = round(time.monotonic() - start, 2)
|
||||
report["conservative_tokens"] = router.guard.used
|
||||
report["life_runs"] = [dict(r) for r in s.query("SELECT status,reason FROM life_run")]
|
||||
report["attempted_model_calls"] = len(report["calls"])
|
||||
output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps({"checks": report["checks"], "elapsed_seconds": report["elapsed_seconds"]}, ensure_ascii=False))
|
||||
return all(report["checks"].values())
|
||||
|
||||
Reference in New Issue
Block a user