-- Agent-specific error serialization is Lua, not a rule in the Python transport. local plugin = {} local completed = nil function plugin.on_event(ctx, event) if event.event == 'run_completed' then if completed then error('duplicate terminal event') end completed = event return {action='hold'} end return {action='pass'} end function plugin.on_end(ctx) if completed then local event = completed completed = nil return {action='replace', events={event}} end end function plugin.on_error(ctx, failure) local payload = nil if failure.last_event and failure.last_event.data then local ok, decoded = pcall(host.json.decode, failure.last_event.data) if ok then payload = decoded end end local sequence = 1 local request_id = ctx.request_id if type(payload) == 'table' then if type(payload.sequence) == 'number' then sequence = payload.sequence + 1 end if type(payload.request_id) == 'string' then request_id = payload.request_id end end return { event = 'run_error', id = tostring(sequence), has_data = true, data = host.json.encode({ request_id = request_id, sequence = sequence, timestamp = failure.timestamp, data = {error_type = failure.error_type}, }), } end return plugin