- Select one storage backend per instance via AGENT_STORAGE, retaining memory as the backward-compatible default. Manage the selected async repository, official checkpointer and connection pools in lifespan; fail startup on unavailable or uninitialized storage without fallback. - Add SQLite and PostgreSQL adapters, schema initialization CLI, sample PostgreSQL compose configuration and a Windows-compatible serve entry. SQLite enforces single-instance file ownership; PostgreSQL uses a dedicated pool for session advisory locks plus version/epoch fencing. - Make repository IO and commit callbacks awaitable. Persist business fields, a complete committed graph snapshot and turn result/version in one business transaction before success. Rebuild execution checkpoints from that snapshot, discarding uncommitted progress after interruption. - Support optional Idempotency-Key on durable chat requests. Replay committed results and return 409 on changed request content; delete removes business/history data and replay records while retaining only an epoch/version tombstone to reject stale commits. - Document migration, database switching boundaries, backup/deployment expectations and crash recovery. Ignore generated database files. Validation: 315 tests passed with real SQLite and isolated PostgreSQL, including abrupt child-process exits before/after business commit; both backends passed CLI migration and actual HTTP server restart smoke. Existing API tests were rechecked after isolating them from user storage. An initial Windows CreateProcess permission failure did not recur in the full diagnostic run; failure assertions were not skipped or relaxed.
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
"""Integration-test child: exit abruptly at a durable turn's commit boundary."""
|
|
import asyncio
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
|
|
from src.agent.graph import build_graph
|
|
from src.backends.chat import ChatInput
|
|
from src.backends.langgraph import LangGraphBackend
|
|
from src.services.app_service import AccidentAppService
|
|
from src.storage.runtime import StorageSettings, open_storage
|
|
|
|
|
|
class LLM:
|
|
async def complete(self, system, user, *, history=None):
|
|
if '【字段提取】' in system:
|
|
return '{"has_injury":false}'
|
|
return '<state>1002</state>crash-boundary-reply'
|
|
|
|
|
|
async def main():
|
|
kind, path, session, boundary = sys.argv[1:]
|
|
settings = StorageSettings(kind=kind, sqlite_path=path,
|
|
postgres_dsn=os.getenv('AGENT_TEST_POSTGRES_DSN',''), pool_size=2)
|
|
async with open_storage(settings) as storage:
|
|
service = AccidentAppService(LangGraphBackend(build_graph(storage.checkpointer), llm=LLM()),
|
|
storage.repository, locks=storage.locks)
|
|
original = storage.repository.commit_turn
|
|
async def crash(*args):
|
|
if boundary=='before':
|
|
os._exit(23)
|
|
await original(*args)
|
|
os._exit(24)
|
|
storage.repository.commit_turn = crash
|
|
await service.chat(ChatInput(session,'继续办理',request_key='crash-key'))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
factory=asyncio.SelectorEventLoop if os.name=='nt' else None
|
|
with asyncio.Runner(loop_factory=factory) as runner:
|
|
runner.run(main())
|