mirror of
https://github.com/morluto/rea
synced 2026-10-28 06:46:47 -04:00
dc022bcd0f
Validate per-response usage totals, retain the negative large-VM comparison and qualify the client executor, cache and investigation-depth differences. Add optional common envelope/stopping guidance without relabeling historical runs. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
57 lines
2.8 KiB
Python
57 lines
2.8 KiB
Python
"""The cost ledger must not confuse MCP batching with model response rounds."""
|
|
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location(
|
|
'summarize_trials', Path(__file__).with_name('summarize-trials.py'))
|
|
ledger = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(ledger)
|
|
|
|
|
|
class ClientObservations(unittest.TestCase):
|
|
def test_distinct_responses_and_dispatch_calls_deduplicate_by_identity(self):
|
|
usage = {'input_tokens': 12, 'cached_input_tokens': 8, 'output_tokens': 2}
|
|
events = [
|
|
{'type': 'token_usage_record', 'payload': {'response_id': 'response', 'usage': usage}},
|
|
{'type': 'response_item', 'payload': {
|
|
'type': 'custom_tool_call', 'name': 'exec', 'call_id': 'dispatch',
|
|
'input': 'Two MCP calls may occur inside this one client dispatch.'}},
|
|
{'type': 'response_item', 'payload': {
|
|
'type': 'function_call', 'name': 'wait', 'call_id': 'wait', 'arguments': '{}'}},
|
|
]
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
(root / 'context-receipt.json').write_text(json.dumps({'status': 'captured'}))
|
|
path = root / 'codex-tool-context.jsonl'
|
|
path.write_text('\n'.join(json.dumps(event) for event in events * 2))
|
|
result = ledger.client_observations(root, usage)
|
|
self.assertEqual(result['model_responses'], 1)
|
|
self.assertEqual(result['tool_calls'], {'exec': 1, 'wait': 1})
|
|
self.assertTrue(result['per_response_usage_matches_summary'])
|
|
with self.assertRaisesRegex(ValueError, 'differs'):
|
|
ledger.client_observations(root, {'input_tokens': 13})
|
|
conflicting = {'type': 'token_usage_record', 'payload': {
|
|
'response_id': 'response', 'usage': {'input_tokens': 13}}}
|
|
with path.open('a') as output:
|
|
output.write('\n' + json.dumps(conflicting))
|
|
with self.assertRaisesRegex(ValueError, 'Conflicting usage'):
|
|
ledger.client_observations(root, usage)
|
|
|
|
def test_missing_or_partial_capture_does_not_claim_zero_calls_or_complete_usage(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
self.assertIsNone(ledger.client_observations(root, {})['model_responses'])
|
|
(root / 'context-receipt.json').write_text(json.dumps({'status': 'partial'}))
|
|
(root / 'codex-tool-context.jsonl').write_text('')
|
|
result = ledger.client_observations(root, {'input_tokens': 100})
|
|
self.assertIsNone(result['model_responses'])
|
|
self.assertIsNone(result['per_response_usage_matches_summary'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|