Schedule consultation bridges on demand and preserve delivery evidence
This commit is contained in:
+92
-1
@@ -10,6 +10,53 @@ import notifier
|
||||
|
||||
|
||||
class NotifierTests(unittest.TestCase):
|
||||
def test_decisions_prefer_fresh_work_but_waiting_items_survive_restart_and_age(self):
|
||||
normal={'notification_id':'ordinary','checkpoint':'half','task':{'id':'work'}}
|
||||
decision={'notification_id':'choice','decision':{'id':'decision'}}
|
||||
state={}
|
||||
self.assertEqual(notifier.schedule_notifications([normal,decision],state,100)[0],decision)
|
||||
# Persist/reload and a continuing stream of fresh decisions cannot starve old work.
|
||||
state=json.loads(json.dumps(state))
|
||||
fresh={'notification_id':'new-choice','decision':{'id':'new'}}
|
||||
self.assertEqual(notifier.schedule_notifications([fresh,normal],state,221)[0],normal)
|
||||
self.assertEqual(state['notification_waiting']['eta:ordinary'],100)
|
||||
|
||||
def test_waiting_age_does_not_bypass_uncertain_target_backpressure(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
cfg,n,path=self.fixture(root);cfg['enforce_backpressure']=True
|
||||
class Client:
|
||||
def call(self,p,data=None):return {'notifications':[n]} if p.endswith('notifications') else {}
|
||||
state={'notification_waiting':{'hook:note1':0}}
|
||||
with patch.object(notifier,'target_ready',return_value=False),patch.object(notifier.subprocess,'run') as run:
|
||||
notifier.deliver(Client(),cfg,state,path)
|
||||
run.assert_not_called()
|
||||
self.assertEqual(json.loads(path.read_text())['notification_waiting']['hook:note1'],0)
|
||||
|
||||
def test_uncertain_reconciliation_rejects_wrong_thread_even_with_exact_input(self):
|
||||
import hashlib
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
cfg,n,path=self.fixture(root)
|
||||
prompt=notifier.notification_text(n)
|
||||
state={'deliveries':{'note1':{'thread_id':cfg['notification_targets']['control'],
|
||||
'input_sha256':hashlib.sha256(prompt.encode()).hexdigest(),'state':'submitting'}}}
|
||||
class Client:
|
||||
acks=[]
|
||||
def call(self,p,data=None):
|
||||
if p.endswith('notifications'):return {'notifications':[n]}
|
||||
self.acks.append(p);return {}
|
||||
class Reader:
|
||||
def __init__(self,*args):pass
|
||||
def __enter__(self):return self
|
||||
def __exit__(self,*args):pass
|
||||
def history(self,thread):return {'id':'different-thread'},[{'id':'turn','items':[
|
||||
{'type':'userMessage','content':[{'type':'text','text':prompt}]}]}]
|
||||
client=Client()
|
||||
with patch.object(notifier,'AppServer',Reader),patch.object(notifier.subprocess,'run') as run:
|
||||
with self.assertRaises(RuntimeError):notifier.deliver(client,cfg,state,path)
|
||||
run.assert_not_called()
|
||||
self.assertEqual(client.acks,[])
|
||||
self.assertEqual(state['deliveries']['note1']['state'],'submitting')
|
||||
|
||||
def fixture(self,root):
|
||||
cfg={'control':{},'notification_targets':{'control':'11111111-1111-4111-8111-111111111111'},'codex':'codex','cwd':root}
|
||||
n={'notification_id':'note1','target':'control','hook':{'source':'test','event_id':'incident','summary':'synthetic failure','severity':'warning','status':'firing'}}
|
||||
@@ -165,13 +212,57 @@ class NotifierTests(unittest.TestCase):
|
||||
'event':{'kind':'fact_answered','request_id':'fact-one','answer':'The prerequisite is verified.'}}
|
||||
class Client:
|
||||
def call(self,p,data=None):return {'notifications':[n]} if p=='/api/decision-notifications' else {'notifications':[]}
|
||||
class Reader:
|
||||
def __init__(self,*args):pass
|
||||
def __enter__(self):return self
|
||||
def __exit__(self,*args):pass
|
||||
def call(self,method,params):
|
||||
return {'thread':{'id':thread}} if method=='thread/read' else {'data':[]}
|
||||
result=SimpleNamespace(returncode=0,stdout='Queued message 22222222-2222-4222-8222-222222222222 for thread '+thread+'.')
|
||||
state={}
|
||||
with patch.object(notifier.subprocess,'run',return_value=result) as run:
|
||||
with patch.object(notifier.subprocess,'run',return_value=result) as run,patch.object(notifier,'AppServer',Reader):
|
||||
notifier.deliver(Client(),cfg,state,path);notifier.deliver(Client(),cfg,state,path)
|
||||
self.assertEqual(run.call_count,1)
|
||||
self.assertIn(thread,run.call_args.args[0])
|
||||
self.assertNotIn(cfg['notification_targets']['control'],run.call_args.args[0])
|
||||
record=state['deliveries']['decision:fact-answer']
|
||||
self.assertEqual(record['decision_id'],'one')
|
||||
self.assertEqual(record['event_kind'],'fact_answered')
|
||||
|
||||
def test_fact_answer_completion_reconciles_empty_outbox_without_resend(self):
|
||||
import hashlib
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
cfg,_,path=self.fixture(root)
|
||||
thread='44444444-4444-4444-8444-444444444444'
|
||||
prompt='fact answer exact input'
|
||||
record={'kind':'decision','decision_id':'one','event_kind':'fact_answered',
|
||||
'thread_id':thread,'input_sha256':hashlib.sha256(prompt.encode()).hexdigest(),
|
||||
'state':'accepted','acked':True}
|
||||
state={'deliveries':{'decision:answer':record}}
|
||||
class Reader:
|
||||
body=prompt
|
||||
identity=thread
|
||||
def __init__(self,*args):pass
|
||||
def __enter__(self):return self
|
||||
def __exit__(self,*args):pass
|
||||
def call(self,method,params):
|
||||
if method=='thread/read':return {'thread':{'id':self.identity}}
|
||||
return {'data':[{'id':'real-turn','status':'completed','completedAt':123,
|
||||
'items':[{'type':'userMessage','content':[{'type':'text','text':self.body}]},
|
||||
{'type':'agentMessage','phase':'final_answer','text':'verified'}]}]}
|
||||
client=SimpleNamespace(call=lambda *args:{'notifications':[]})
|
||||
with patch.object(notifier,'AppServer',Reader),patch.object(notifier.subprocess,'run') as run:
|
||||
Reader.body=prompt+' changed'
|
||||
notifier.deliver(client,cfg,state,path)
|
||||
self.assertFalse(record.get('processing_done'))
|
||||
Reader.body=prompt;Reader.identity='wrong-thread'
|
||||
with self.assertRaises(RuntimeError):notifier.deliver(client,cfg,state,path)
|
||||
self.assertFalse(record.get('processing_done'))
|
||||
Reader.identity=thread
|
||||
notifier.deliver(client,cfg,state,path)
|
||||
run.assert_not_called()
|
||||
self.assertEqual(record['turn_id'],'real-turn')
|
||||
self.assertTrue(json.loads(path.read_text())['deliveries']['decision:answer']['processing_done'])
|
||||
|
||||
def test_review_notice_is_concise_and_submission_is_not_acceptance(self):
|
||||
n={'notification_id':'notice','decision':{'id':'one','title':'Plan','options':[{'id':'a','label':'Private option detail'}]},
|
||||
|
||||
Reference in New Issue
Block a user