120 lines
6.5 KiB
Python
120 lines
6.5 KiB
Python
"""Four bounded calls, synthetic data only, no server, no production DB.
|
|
|
|
Run explicitly with --real. No retries or model/config changes on a failure.
|
|
"""
|
|
import argparse
|
|
import asyncio
|
|
import json
|
|
import tempfile
|
|
import time
|
|
from contextlib import aclosing
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
import httpx
|
|
|
|
from cybergril.api.app import create_app
|
|
from cybergril.life.model import PersistentBudget
|
|
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, "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):
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://isolated") as c:
|
|
response = await c.post("/api/companion", json={"name": "隔离合成人物", "birth": "2000-10-18", "hour": "亥时", "relation": "朋友"})
|
|
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"]),
|
|
("word_only", "你想聊聊‘" + (life["place"] or "日常") + "’这个词吗?我不是在说自己做过什么。"),
|
|
):
|
|
tid = str(uuid4())
|
|
with s.conn:
|
|
row = s.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES(?,'companion',?,?,'sent')",
|
|
(int(time.time() * 1000), text, tid))
|
|
receipt = app.state.sharing.prepare(row.lastrowid, tid, app.state.epoch, text, [life["id"]])
|
|
await app.state.sharing.judge(receipt["message_id"])
|
|
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())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--real", action="store_true", required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
raise SystemExit(0 if asyncio.run(run(args.output)) else 1)
|