feat(work): add signed task progress and durable status cards
This commit is contained in:
@@ -658,3 +658,17 @@ def test_prepare_update_file_feature_is_explicit_and_rollback_restores_parent_fl
|
||||
assert current['services']['connector']['environment']['HN_WORK_FILES_ENABLED']=='true'
|
||||
assert rollback['services']['connector']['environment']==old['services']['connector']['environment']
|
||||
assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes()
|
||||
|
||||
|
||||
def test_prepare_update_cards_are_opt_in_and_rollback_keeps_parent_environment(scenario):
|
||||
parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64
|
||||
for name in ('connector','router'):scenario['roles'][name]['image']=image
|
||||
deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']})
|
||||
output=root.parent/'update-cards'
|
||||
deploy.prepare_update(argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output),enable_task_cards=True))
|
||||
current=json.loads((output/'compose.broker.json').read_text())
|
||||
rollback=json.loads((output/'compose.rollback.json').read_text())
|
||||
old=json.loads((root/'compose.broker.json').read_text())
|
||||
assert current['services']['connector']['environment']['HN_WORK_TASK_CARDS_ENABLED']=='true'
|
||||
assert rollback['services']['connector']['environment']==old['services']['connector']['environment']
|
||||
assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes()
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Joint source test: encrypted PC journal -> signed ECS progress -> card projection."""
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import types
|
||||
import httpx
|
||||
import pytest
|
||||
from test_credential_api import env,wire
|
||||
from test_task_delivery import task
|
||||
from test_task_cards import setup_cards
|
||||
|
||||
|
||||
async def test_native_states_reach_signed_server_and_existing_card(env,tmp_path,monkeypatch):
|
||||
client=Path(__file__).resolve().parents[3]/'hainuo-build-remote-C-20261112/integrations/work-relay/service/relay'
|
||||
if not client.is_dir():pytest.skip('paired client source not present')
|
||||
package=types.ModuleType('joint_pc_relay');package.__path__=[str(client)]
|
||||
monkeypatch.setitem(sys.modules,'joint_pc_relay',package)
|
||||
journal_module=importlib.import_module('joint_pc_relay.task_journal')
|
||||
native=importlib.import_module('joint_pc_relay.hainuo1065')
|
||||
monkeypatch.setattr(journal_module,'protect',lambda data:b'test:'+data)
|
||||
monkeypatch.setattr(journal_module,'unprotect',lambda data:data[5:])
|
||||
clock=[0.0]
|
||||
# Patch only the native module clock, not shared time.monotonic used by event loop.
|
||||
monkeypatch.setattr(native,'time',types.SimpleNamespace(monotonic=lambda:clock[0],time=__import__('time').time))
|
||||
async def no_wait(_):clock[0]+=1
|
||||
monkeypatch.setattr(native,'asyncio',types.SimpleNamespace(sleep=no_wait))
|
||||
context={'task_id':'task-durable','conversation_id':'synthetic-conversation'}
|
||||
ledger=journal_module.TaskJournal(tmp_path);ledger.begin(context,'synthetic')
|
||||
task(env);cards,calls,card_clock=setup_cards(env,monkeypatch)
|
||||
states=iter(['running','waiting_for_approval','running','completed']);reported=[];posts=[]
|
||||
run_id='run_'+'a'*32
|
||||
def handler(request):
|
||||
if request.method=='POST':posts.append(request);return httpx.Response(202,json={'run_id':run_id})
|
||||
return httpx.Response(200,json={'run_id':run_id,'status':next(states),'output':'synthetic result'})
|
||||
config=tmp_path/'native.json';config.write_text(__import__('json').dumps({'url':'http://127.0.0.1:8643','api_key':'fake-'*10,'mode':'text-only'}))
|
||||
adapter=native.Hainuo1065Agent(config,transport=httpx.MockTransport(handler));adapter.model='synthetic'
|
||||
async def progress():
|
||||
saved=ledger.get(context);path='/work/v2/tasks/progress'
|
||||
response=await env['client'].post(path,json=wire(env,path,task_id=context['task_id'],
|
||||
execution_state=saved['execution_state'],status_sequence=saved['status_sequence']))
|
||||
assert response.status_code==200,response.text
|
||||
reported.append(response.json()['execution_state'])
|
||||
card_clock[0]+=6;await cards.flush()
|
||||
try:
|
||||
assert await adapter._execute_run('synthetic',{'X-Hermes-Session-Id':'synthetic-session'},progress,journal=ledger,context=context)=='synthetic result'
|
||||
assert reported==['running','waiting_for_approval','running','completed']
|
||||
assert ledger.get(context)['status_sequence']==4
|
||||
assert len(posts)==1
|
||||
assert [call[0] for call in calls]==['create','update','update','update']
|
||||
assert len(env['platform'].replies)==0 # Execution completion is not result delivery.
|
||||
finally:await adapter.close()
|
||||
@@ -0,0 +1,154 @@
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
import httpx
|
||||
import pytest
|
||||
from test_credential_api import env,wire
|
||||
from test_task_delivery import task,upload
|
||||
from relay.task_cards import TaskCards,card_content
|
||||
from relay.platform import Feishu,PlatformError
|
||||
|
||||
|
||||
def setup_cards(env,monkeypatch):
|
||||
calls=[];clock=[time.time()]
|
||||
async def create(message,card,delivery_id,*,token):
|
||||
calls.append(('create',message,card,delivery_id));return 'om_card'
|
||||
async def update(message,card,*,token):calls.append(('update',message,card))
|
||||
monkeypatch.setattr(env['platform'],'reply_task_card',create,raising=False)
|
||||
monkeypatch.setattr(env['platform'],'update_task_card',update,raising=False)
|
||||
return TaskCards(env['store'].lifecycle,env['platform'],'worker-test',clock=lambda:clock[0]),calls,clock
|
||||
|
||||
|
||||
async def report(env,state,sequence):
|
||||
path='/work/v2/tasks/progress'
|
||||
response=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state=state,status_sequence=sequence))
|
||||
assert response.status_code==200,response.text
|
||||
|
||||
|
||||
async def test_card_created_once_updates_in_order_and_delivery_separate(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
await report(env,'running',1);await cards.flush();await cards.flush()
|
||||
assert len(calls)==1 and calls[0][0]=='create' and calls[0][1]=='om-synthetic'
|
||||
await report(env,'waiting_for_approval',2)
|
||||
await cards.flush();assert len(calls)==1 # Update throttle.
|
||||
clock[0]+=6;await cards.flush()
|
||||
assert calls[-1][0]=='update' and '等待本人确认' in json.dumps(calls[-1],ensure_ascii=False)
|
||||
assert 'button' not in json.dumps(calls[-1])
|
||||
await report(env,'completed',3);clock[0]+=6;await cards.flush()
|
||||
text=json.dumps(calls[-1],ensure_ascii=False)
|
||||
assert '执行完成' in text and '尚未接收结果' in text and '结果已发送' not in text
|
||||
await upload(env);await env['app'].state.deliveries.flush()
|
||||
clock[0]+=6;await cards.flush()
|
||||
assert '结果已发送到当前会话' in json.dumps(calls[-1],ensure_ascii=False)
|
||||
assert len([c for c in calls if c[0]=='create'])==1
|
||||
|
||||
|
||||
async def test_unknown_create_receipt_is_durable_and_never_recreated(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
async def lost(*args,**kwargs):calls.append(('lost',));raise TimeoutError()
|
||||
monkeypatch.setattr(env['platform'],'reply_task_card',lost)
|
||||
await report(env,'running',1);await cards.flush()
|
||||
clock[0]+=65
|
||||
reopened=TaskCards(env['store'].lifecycle,env['platform'],'new-worker',clock=lambda:clock[0])
|
||||
await report(env,'completed',2);await reopened.flush()
|
||||
assert len(calls)==1
|
||||
assert env['store'].db.execute('SELECT status FROM work_task_cards_v3').fetchone()['status']=='unknown'
|
||||
await upload(env);await env['app'].state.deliveries.flush()
|
||||
assert len(env['platform'].replies)==1 # Card failure never suppresses result text.
|
||||
|
||||
|
||||
async def test_failed_update_retries_same_card_with_newest_state(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
await report(env,'running',1);await cards.flush()
|
||||
original=env['platform'].update_task_card
|
||||
async def lost(*args,**kwargs):calls.append(('lost-update',args[0]));raise TimeoutError()
|
||||
monkeypatch.setattr(env['platform'],'update_task_card',lost)
|
||||
await report(env,'waiting_for_approval',2);clock[0]+=6;await cards.flush()
|
||||
monkeypatch.setattr(env['platform'],'update_task_card',original)
|
||||
await report(env,'failed',3);clock[0]+=6;await cards.flush()
|
||||
assert calls[-1][0:2]==('update','om_card')
|
||||
assert '执行失败' in json.dumps(calls[-1],ensure_ascii=False)
|
||||
assert len([c for c in calls if c[0]=='create'])==1
|
||||
|
||||
|
||||
async def test_unbind_during_token_preflight_prevents_card_send(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
async def token():env['store'].unbind('ou_a');return 'fake'
|
||||
monkeypatch.setattr(env['platform'],'tenant_token',token)
|
||||
await cards.flush();assert not calls
|
||||
|
||||
|
||||
async def test_two_workers_and_stale_snapshot_cannot_send_old_state(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
await report(env,'running',1)
|
||||
paused=asyncio.Event();resume=asyncio.Event()
|
||||
original=env['platform'].reply_task_card
|
||||
async def create(*args,**kwargs):paused.set();await resume.wait();return await original(*args,**kwargs)
|
||||
monkeypatch.setattr(env['platform'],'reply_task_card',create)
|
||||
first=asyncio.create_task(cards.flush());await paused.wait()
|
||||
other=TaskCards(env['store'].lifecycle,env['platform'],'worker-other',clock=lambda:clock[0])
|
||||
await other.flush();assert not calls
|
||||
resume.set();await first
|
||||
assert len(calls)==1
|
||||
await report(env,'waiting_for_approval',2)
|
||||
async def token():
|
||||
# A new authenticated status arrives while card worker awaits preflight.
|
||||
await report(env,'completed',3);return 'fake'
|
||||
monkeypatch.setattr(env['platform'],'tenant_token',token)
|
||||
clock[0]+=6;await other.flush()
|
||||
assert '执行完成' in json.dumps(calls[-1],ensure_ascii=False)
|
||||
|
||||
|
||||
async def test_stale_creation_claim_is_unknown_but_stale_update_can_recover(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
db=env['store'].db
|
||||
with db:db.execute('INSERT INTO work_task_cards_v3 VALUES(?,?,?,?,?,?)',('task-durable','','','sending','dead',clock[0]-61))
|
||||
await cards.flush();await cards.flush();assert not calls
|
||||
assert db.execute('SELECT status FROM work_task_cards_v3').fetchone()['status']=='unknown'
|
||||
with db:db.execute("UPDATE work_task_cards_v3 SET message_id='om_card',status='sending',updated_at=?",(clock[0]-61,))
|
||||
await cards.flush();await cards.flush()
|
||||
assert calls[0][0:2]==('update','om_card')
|
||||
|
||||
|
||||
def test_card_projection_never_exposes_task_body_or_markdown_controls():
|
||||
data=card_content('synthetic-task','waiting_for_approval',None)
|
||||
text=json.dumps(data,ensure_ascii=False)
|
||||
assert 'button' not in text and 'markdown' not in text
|
||||
assert data['config']['enable_forward'] is False
|
||||
|
||||
|
||||
async def test_card_uses_task_bound_source_and_skips_foreign_and_expired(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
with env['store'].db:
|
||||
env['store'].db.execute('UPDATE work_tasks_v2 SET deadline=?',(clock[0]-1,))
|
||||
await cards.flush();assert not calls
|
||||
with env['store'].db:
|
||||
env['store'].db.execute("UPDATE work_tasks_v2 SET deadline=?,binding_generation=999",(clock[0]+300,))
|
||||
await cards.flush();assert not calls
|
||||
|
||||
|
||||
async def test_timeout_projects_unknown_without_claiming_failure(env,monkeypatch):
|
||||
task(env);cards,calls,clock=setup_cards(env,monkeypatch)
|
||||
await report(env,'running',1);await cards.flush()
|
||||
with env['store'].db:env['store'].db.execute("UPDATE work_tasks_v2 SET status='unknown'")
|
||||
clock[0]+=6;await cards.flush()
|
||||
text=json.dumps(calls[-1],ensure_ascii=False)
|
||||
assert '状态待核对' in text and '执行失败' not in text
|
||||
|
||||
|
||||
async def test_platform_card_wire_format_and_receipt_validation(env):
|
||||
seen=[]
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
return httpx.Response(200,json={'code':0,'data':{'message_id':'om_receipt'}})
|
||||
platform=Feishu(env['settings'],transport=httpx.MockTransport(handler))
|
||||
card=card_content('synthetic','running',None)
|
||||
try:
|
||||
assert await platform.reply_task_card('om_source',card,'a'*32,token='fake')=='om_receipt'
|
||||
await platform.update_task_card('om_receipt',card,token='fake')
|
||||
body=json.loads(seen[0].content)
|
||||
assert body['msg_type']=='interactive' and json.loads(body['content'])==card
|
||||
assert seen[1].method=='PATCH' and seen[1].url.path.endswith('/om_receipt')
|
||||
with pytest.raises(PlatformError):await platform.reply_task_card('../other',card,'a'*32,token='fake')
|
||||
assert len(seen)==2
|
||||
finally:await platform.close()
|
||||
@@ -16,6 +16,40 @@ async def upload(env,turn='task-durable',text='synthetic-private-result'):
|
||||
return await env['client'].post(path,json=wire(env,path,task_id=turn,result=text))
|
||||
|
||||
|
||||
async def test_execution_status_order_replay_terminal_and_delivery_are_separate(env):
|
||||
task(env)
|
||||
path='/work/v2/tasks/progress'
|
||||
async def report(state,sequence,turn='task-durable'):
|
||||
return await env['client'].post(path,json=wire(env,path,task_id=turn,
|
||||
execution_state=state,status_sequence=sequence))
|
||||
assert (await report('running',1)).json()['execution_state']=='running'
|
||||
waiting=await report('waiting_for_approval',2)
|
||||
assert waiting.json()['status_sequence']==2
|
||||
assert (await report('running',1)).json()['execution_state']=='waiting_for_approval'
|
||||
assert (await report('waiting_for_approval',2)).status_code==200
|
||||
assert (await report('running',2)).status_code==409
|
||||
completed=await report('completed',3)
|
||||
assert completed.json()['execution_state']=='completed'
|
||||
assert not completed.json()['result_received']
|
||||
assert (await report('running',4)).status_code==409
|
||||
await upload(env)
|
||||
assert (await report('completed',3)).json()['result_received']
|
||||
|
||||
|
||||
async def test_execution_state_auth_binding_and_parameter_validation(env):
|
||||
task(env)
|
||||
path='/work/v2/tasks/progress'
|
||||
missing=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state='running'))
|
||||
assert missing.status_code==422
|
||||
task(env,'foreign-task','ou_b','b'*32)
|
||||
foreign=await env['client'].post(path,json=wire(env,path,task_id='foreign-task',execution_state='running',status_sequence=1))
|
||||
assert foreign.status_code==403
|
||||
env['store'].unbind('ou_a')
|
||||
revoked=await env['client'].post(path,json=wire(env,path,task_id='task-durable',execution_state='running',status_sequence=1))
|
||||
assert revoked.status_code==403
|
||||
assert not env['store'].db.execute('SELECT * FROM work_execution_state_v3').fetchall()
|
||||
|
||||
|
||||
async def test_result_is_encrypted_durable_and_delivered_once_after_worker_loss(env):
|
||||
task(env)
|
||||
api=env['app'].state.deliveries
|
||||
|
||||
Reference in New Issue
Block a user