57 lines
2.8 KiB
Python
57 lines
2.8 KiB
Python
"""Offline storage-limit stop intent regression."""
|
|
import asyncio
|
|
from control.accounts import Accounts, token_hash
|
|
from control.chat_store import ChatStore
|
|
from control.chat_runs import ChatRuns
|
|
|
|
def test_event_capacity_failure_persists_stop_and_reserved_terminal(tmp_path):
|
|
async def scenario():
|
|
accounts = Accounts(str(tmp_path/'limit.db'))
|
|
owner = accounts.create('test','Test','admin','offline-test-password')
|
|
store = ChatStore(accounts)
|
|
cid = store.create_conversation(owner['id'])['id']
|
|
rid = store.send(owner['id'],cid,'Hi','key')['run']['id']
|
|
store.update_run(owner['id'],rid,'running')
|
|
runs = ChatRuns(accounts,store,adapter=object())
|
|
runs.metadata(owner['id'],rid,{'conversation_id':cid,'payload':{},'upstream_id':'upstream','assistant_id':None})
|
|
original = store.append_event
|
|
def limited(owner,rid,event,data):
|
|
if event == 'progress':
|
|
raise ValueError('storage limit')
|
|
return original(owner,rid,event,data)
|
|
store.append_event = limited
|
|
try:
|
|
await runs.emit(owner['id'],rid,'progress',{'status':'working'})
|
|
except ValueError:
|
|
pass
|
|
metadata = runs.metadata(owner['id'],rid)
|
|
assert metadata['stop_requested'] and metadata['storage_failed']
|
|
assert await runs.consume_status(owner['id'],rid,metadata,{'status':'cancelled'})
|
|
assert store.get_run(owner['id'],rid)['run']['status'] == 'failed'
|
|
assert store.events(owner['id'],rid)[-1]['event'] == 'done'
|
|
accounts.close()
|
|
asyncio.run(scenario())
|
|
|
|
|
|
def test_cancel_key_before_admission_never_starts_and_is_owner_scoped(tmp_path):
|
|
async def scenario():
|
|
accounts = Accounts(str(tmp_path/'cancel.db'))
|
|
user = accounts.create('test','Test','admin','offline-test-password')
|
|
other = accounts.create('other','Other','user','offline-test-password')
|
|
token, _ = accounts.issue_session(user['id'],3600,user['password_hash'])
|
|
store = ChatStore(accounts)
|
|
cid = store.create_conversation(user['id'])['id']
|
|
runs = ChatRuns(accounts,store,adapter=object())
|
|
assert await runs.cancel_operation(user['id'],cid,'uncertain-key') == {'ok':True,'run':None}
|
|
operation = await runs.send(user['id'],cid,token,content='Hi',idempotency_key='uncertain-key')
|
|
assert operation['run']['status'] == 'cancelled'
|
|
assert runs.tasks == {}
|
|
assert (await runs.send(user['id'],cid,token,content='Hi',idempotency_key='uncertain-key'))['run']['id'] == operation['run']['id']
|
|
try:
|
|
await runs.cancel_operation(other['id'],cid,'uncertain-key')
|
|
assert False, 'Foreign owner cancellation was accepted'
|
|
except KeyError:
|
|
pass
|
|
accounts.close()
|
|
asyncio.run(scenario())
|