contexts.extend(e for e in parsed if e.get('type') in ('session_meta', 'turn_context'))
identity = [c['payload'][key] for c in contexts for key in ('creator_user_id', 'creator_account_id') if c.get('payload', {}).get(key)]
secrets = [*secrets, *identity]
result['session_contexts'] = contexts
result['resolved_models'] = sorted({c['payload']['model'] for c in contexts if c.get('type') == 'turn_context'})
result['resolved_efforts'] = sorted({c['payload'].get('effort') for c in contexts if c.get('type') == 'turn_context'}, key=str)
messages = [e['item']['text'] for e in events if e.get('type') == 'item.completed' and e.get('item', {}).get('type') == 'agent_message']
result['response'] = messages[-1] if messages else ''
tool_items = [e for e in events if e.get('type') in ('item.started', 'item.completed') and e.get('item', {}).get('type') not in ('agent_message', 'reasoning')]
result['tool_items'] = tool_items
result['status'] = 'completed'
if result['returncode'] or result['timeout'] or not any(e.get('type') == 'turn.completed' for e in events):