feat(work): recover text and attachment delivery independently

This commit is contained in:
ex_tianzy
2026-11-23 14:49:51 +08:00
parent 1b3fded4ae
commit 3f4b8f983b
7 changed files with 210 additions and 24 deletions
+82
View File
@@ -83,6 +83,88 @@ async def test_artifact_handoff_encrypted_idempotent_and_replies_to_original_onl
assert env['store'].db.execute("SELECT status,ciphertext FROM work_files_v1").fetchone()['status']=='sent'
@pytest.mark.parametrize('second_status',['uploaded','sending'])
async def test_restart_recovers_only_confirmed_unsent_attachment(env,monkeypatch,second_status):
import httpx
from relay.connector import create_app
_,calls=fake_platform(env,monkeypatch);create_task(env)
result_path='/work/v2/tasks/result'
response=await env['client'].post(result_path,json=wire(env,result_path,task_id='task-files',result='synthetic result'))
assert response.status_code==200
await env['app'].state.deliveries.flush()
one=await upload(env,name='first.docx',artifact='artifact-01')
assert one.status_code==200
api=env['app'].state.files
await api.flush();await api.flush()
two=await upload(env,name='second.xlsx',artifact='artifact-02',raw=b'second synthetic file')
assert two.status_code==200
await api.flush()
assert [r[0] for r in calls]==['upload','reply_file','upload']
if second_status=='sending':
with api.db:
api.db.execute("UPDATE work_files_v1 SET status='sending',updated=? WHERE file_id=? AND status='uploaded'",
(time.time()-121,two.json()['file_id']))
second=create_app(env['settings'],env['redis'],env['platform'])
try:
for _ in range(2):
await second.state.files.flush()
await second.state.deliveries.flush()
async with httpx.AsyncClient(transport=httpx.ASGITransport(second),base_url='https://relay.example.test') as http:
path='/work/v2/tasks/state'
status=await http.post(path,json=wire(env,path,task_id='task-files'))
assert status.status_code==200
receipt=status.json()['delivery']
assert receipt['text']=={'status':'replied','platform_sent':True}
assert receipt['protocol']=='work-delivery-v1'
files={e['name']:(e['status'],e['platform_sent']) for e in receipt['attachments']}
assert files=={'first.docx':('sent',True),'second.xlsx':
(('sent',True) if second_status=='uploaded' else ('unknown',False))}
from relay.task_cards import TaskCards
cards=TaskCards(second.state.store.lifecycle,env['platform'],'snapshot-test')
cards.file_receipts=second.state.files.output_receipts
rendered=json.dumps(cards.snapshot(second.state.store.lifecycle.task('task-files')),ensure_ascii=False)
assert ('1 份附件的送达结果待核对' in rendered)==(second_status=='sending')
denied=await http.post(path,json=wire(env,path,host='b'*32,task_id='task-files'))
assert denied.status_code==403
finally:
second.state.store.close()
assert env['platform'].replies==[('om-original','synthetic result')]
assert [r[0] for r in calls]==(['upload','reply_file','upload','reply_file']
if second_status=='uploaded' else ['upload','reply_file','upload'])
async def test_local_file_integrity_failure_is_not_a_platform_unknown(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);create_task(env)
result=await upload(env)
assert result.status_code==200
api=env['app'].state.files
with api.db:api.db.execute("UPDATE work_files_v1 SET ciphertext='invalid' WHERE file_id=?",(result.json()['file_id'],))
await api.flush()
await api.flush()
rows=api.output_receipts(env['store'].lifecycle.task('task-files'))
assert rows[0]['status']=='failed' and not rows[0]['platform_sent']
assert not calls
async def test_attachment_expiry_cleans_bytes_without_erasing_uncertain_delivery(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);task=create_task(env)
first=await upload(env,artifact='artifact-01')
second=await upload(env,artifact='artifact-02',name='ambiguous.txt')
assert first.status_code==second.status_code==200
api=env['app'].state.files
with api.db:
api.db.execute("UPDATE work_files_v1 SET expires=?",(time.time()-1,))
api.db.execute("UPDATE work_files_v1 SET status='sending',platform_key='synthetic' WHERE file_id=?",
(second.json()['file_id'],))
await api.flush()
assert {x['name']:x['status'] for x in api.output_receipts(task)}=={
'result.txt':'expired','ambiguous.txt':'unknown'}
assert all(not r['ciphertext'] and not r['platform_key'] for r in
api.db.execute("SELECT ciphertext,platform_key FROM work_files_v1").fetchall())
assert not calls
async def test_bad_digest_filename_size_and_stale_binding_are_rejected(env,monkeypatch):
fake_platform(env,monkeypatch);create_task(env)
assert (await upload(env,sha256='0'*64)).status_code==409