83 lines
4.3 KiB
Python
83 lines
4.3 KiB
Python
"""No model/network calls: process and installed Hermes emitter contract."""
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
import runner as r
|
|
|
|
class ProfileProtocolTests(unittest.TestCase):
|
|
def execute(self, source):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
return r.execute([sys.executable, '-c', source], 'private prompt', dict(os.environ), Path(d), lambda: None, time.monotonic()+3)
|
|
|
|
def test_nonzero_preserves_only_safe_category(self):
|
|
source = "import json,sys; print(json.dumps({'type':'result','exit_code':1,'text':'PRIVATE','error':'credentials or agent init failed PRIVATE'})); sys.exit(1)"
|
|
with self.assertRaises(r.RunnerError) as caught:
|
|
self.execute(source)
|
|
self.assertEqual(caught.exception.code, 'PROFILE_INIT_FAILED')
|
|
self.assertNotIn('PRIVATE', str(caught.exception))
|
|
|
|
def test_corrupt_stream_has_category(self):
|
|
with self.assertRaises(r.RunnerError) as caught:
|
|
self.execute("print('PRIVATE banner')")
|
|
self.assertEqual(caught.exception.code, 'PROFILE_PROTOCOL_INVALID')
|
|
|
|
def test_preclaim_cli_rejects_unsupported_protocol(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
fake = Path(d)/'cli'
|
|
fake.write_text('#!/bin/sh\nprintf "%s\\n" "usage: old chat"\n')
|
|
fake.chmod(0o700)
|
|
with self.assertRaises(r.RunnerError) as caught:
|
|
r.cli_preflight(str(fake), dict(os.environ), Path(d))
|
|
self.assertEqual(caught.exception.code, 'CLI_PROTOCOL_UNSUPPORTED')
|
|
|
|
def test_prompt_respects_installed_extract_floor(self):
|
|
source=(ROOT/'runner.py').read_text()
|
|
self.assertIn('char_limit=2000',source)
|
|
self.assertNotIn('char_limit=1000',source)
|
|
|
|
def test_wall_clock_category(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with self.assertRaises(r.RunnerError) as caught:
|
|
r.execute([sys.executable,'-c','import time;time.sleep(5)'], '', dict(os.environ), Path(d), lambda:None, time.monotonic()+.05)
|
|
self.assertEqual(caught.exception.code, 'PROFILE_TIMEOUT')
|
|
|
|
def test_preflight_checks_cli_before_claim(self):
|
|
from unittest.mock import patch
|
|
with tempfile.TemporaryDirectory() as d:
|
|
home=Path(d)
|
|
for profile in r.PROFILES: (home/'profiles'/profile).mkdir(parents=True)
|
|
real_stat=Path.stat
|
|
def hermes_stat(path, *args, **kwargs):
|
|
fields=list(real_stat(path,*args,**kwargs)); fields[4]=10000
|
|
return os.stat_result(fields)
|
|
with patch.object(r.os, 'getuid', return_value=10000), patch.object(r.Path, 'stat', hermes_stat), patch.object(r, 'cli_preflight', side_effect=r.RunnerError('CLI unsupported',code='CLI_PROTOCOL_UNSUPPORTED')) as check:
|
|
with self.assertRaises(r.RunnerError): r.preflight(home)
|
|
self.assertTrue(check.called)
|
|
|
|
def test_installed_emitter_without_model(self):
|
|
path = Path(os.environ.get('HERMES_STREAM_JSON_SOURCE', '/opt/hermes/hermes_cli/stream_json.py'))
|
|
if not path.exists(): self.skipTest('set HERMES_STREAM_JSON_SOURCE to installed emitter')
|
|
spec = importlib.util.spec_from_file_location('installed_emitter', path)
|
|
module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module)
|
|
stream = io.StringIO()
|
|
with contextlib.redirect_stdout(stream), contextlib.redirect_stderr(io.StringIO()):
|
|
emitter = module.StreamJsonEmitter(model='contract-only', session_id='contract-only')
|
|
emitter.on_tool_progress('tool.started', 'web_extract', args={'urls':['https://www.avma.org/resources']}, tool_call_id='call1')
|
|
emitter.on_tool_progress('tool.completed', 'web_extract', result=json.dumps({'results':[{'url':'https://www.avma.org/resources','content':'contract-only'}]}), tool_call_id='call1')
|
|
emitter.emit_result({'final_response':'contract-only'})
|
|
answer, events = self.execute('import sys; sys.stdout.write('+repr(stream.getvalue())+')')
|
|
self.assertEqual(answer, 'contract-only')
|
|
self.assertEqual(events[1]['tool_call_id'], events[2]['tool_call_id'])
|
|
|
|
if __name__ == '__main__': unittest.main()
|